• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 647
  • Last Modified:

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?
0
Juniper786
Asked:
Juniper786
  • 3
  • 2
  • 2
  • +1
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now