Is there any way to read a binary file into n-bit array?
There is a file that I want read which consists of 10-bit unsigned integers
I want to read them into an int arrray. Is there any way for this?
Thanx

June 19th, 2013, 06:20 AM

angstrem

Re: How to read file into n-bit array

There's not 'bit' datatype in Java. Hence, if you really want to use bits, you'll have to use bytes or booleans or chars or String or something (though booleans' size is 1 byte (or even bigger, I've heard once that it depends on the JVM implementation...)).
To do what you want you can use the following algorithm:
1. Read all the bytes from the file into an array
2. Define a function that would take a byte and return it's bits in a chosen format. Btw, consider Integer.toBinaryString(), which takes an integer and returns a string with it's bits.
3. Apply the (2) function to (1) array in order to represent bits of the file in memory
4. Parse (3) datastructure in a way that corresponds your needs

June 19th, 2013, 03:53 PM

derekxec

Re: How to read file into n-bit array

i dont know if its just me but i have found it hard to work with unsigned integers in java :(

June 20th, 2013, 01:55 PM

angstrem

Re: How to read file into n-bit array

Is it so difficult, for example, to create a separate class to deal with that data type?