Reading a File into a a Byte Array

I have a file of 32 bit floating point numbers in binary that I want to read into a byte array in Java. How would I accomplish this?

I am using the File class and a FileInputStream to read the file but I am not sure about how I would read this correctly into a byte array. I would then like to loop through the array and display the values within it in Hex form or as a char.

I have been told to use a bitwise operation "& 0xff" to ensure the values in the array are correct - I am confused as to why this should be. Any explaination would be welcome.

Any advice would be appreciated?
Juniper786Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cmalakarCommented:
0
ksivananthCommented:
there are limitations to read into byte array... use channel and byte buffer!

http://www.developer.com/java/other/article.php/1481941
http://www.developer.com/java/article.php/10922_1449271_3
http://www.exampledepot.com/egs/java.nio/Buf2Array.html?l=rel

and here is the code for converting byte array into hex,

http://www.devx.com/tips/Tip/13540
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

ksivananthCommented:
here is another way of converting a byte into hex,
public static String byteToHex(byte b){
  int i = b & 0xFF;
  return Integer.toHexString(i);
}
0
Juniper786Author Commented:
Would it be OK to do the following after using the code provided by cmalakar and ysnky:

for(int i = 0; i < bytearray.length; i++){
bytearray [i] = bytearray[i & 0xff];
}

I suppose I am confused about the bitwise operation and why it is needed??  -:)
0
ysnkyCommented:
>>I suppose I am confused about the bitwise operation and why it is needed??
to mask the bits you dont want with an bitwise and: 0xFF & 


have a look at this (the same case);
http://forum.java.sun.com/thread.jspa?threadID=601733&messageID=3227847



try this code;

	public static String byte2Hex(byte b) {
		
		String str = Integer.toHexString(0xff & b);
		return (str.length() < 2 ? "0" : "") + str;		
	}
 
 
 
	String[] strAry = new String[bytearray.length];
	for(int i = 0; i < bytearray.length; i++){
		strAry[i] = byte2Hex(bytearray[i]);
	}

Open in new window

0
cmalakarCommented:
If you dont use bitwise operation, then the byte values greater than 127 will result in  following values, when you display in hex form..

for 127 --> hex value (7F)
for 128 --> hex value (ffffff80) //Actually first 6 f's are not required
for 129 --> hex value (ffffff81) //Actually first 6 f's are not required

If we use bitwise (ie., 0xFF &  byteValue ), we get the following correct values

for 127 --> hex value (7F)
for 128 --> hex value (80)
for 129 --> hex value (81)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Juniper786Author Commented:
Ah Ok - thank you. The replies have helped me understand the issues. I was getting the ffffff values in my display.

One final clarification - if the file that is being read into the byte array is of even parity presumably this would require some further manipulation? From what I understand so far I believe this would give false results during the conversion to Hex if the even parity is not taken into account and the & 0xff is the only manipulation that takes place? Am I right in thinking so?

Thank you in advance
0
Juniper786Author Commented:
Thanks for the help - sorry for the belated reply
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.