Results 1 to 6 of 6
- 02-26-2010, 05:09 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 3
- Rep Power
- 0
Need help converting int to a 4 byte array
Hello all. I apologize in advance if this information is readily available, because I haven't been able to find a definitive answer to this question. I need to convert an int to a big endian 4 byte-array. I have the following code, which I'm fairly sure works, but it's the big endian part I'm not sure about. Can anyone confirm that this code snippet will do what I expect:
Thank you very much in advance for your help.Java Code:public static final byte[] intToByteArray(int value) { return new byte[] { (byte)(value & 0xff), (byte)(value >> 8 & 0xff), (byte)(value >> 16 & 0xff), (byte)(value >>> 24) }; } }
-Kennedy
- 02-26-2010, 05:12 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,411
- Blog Entries
- 7
- Rep Power
- 17
- 02-26-2010, 05:16 PM #3
Member
- Join Date
- Feb 2010
- Posts
- 3
- Rep Power
- 0
Jos,
Are you saying that I should reverse the order of the elements in the array? It sounds like you are saying that the following would be big endian, is that true?
Thanks again.Java Code:public static final byte[] intToByteArray(int value) { return new byte[] { (byte)(value >>> 24), (byte)(value >> 16 & 0xff), (byte)(value >> 8 & 0xff), (byte)(value & 0xff) }; }
-Kennedy
- 02-26-2010, 05:22 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,411
- Blog Entries
- 7
- Rep Power
- 17
- 02-26-2010, 07:32 PM #5
Member
- Join Date
- Feb 2010
- Posts
- 3
- Rep Power
- 0
Jos,
Thank you again for your replies. If you have a moment, please take a look at the following methods. The intent is that these all output data in Big Endian format. However, based on your statements above, I think that they are actually providing Little Endian data.
Java Code:public static int parseUnsignedShort(byte[] data, int offset) { int returnValue = 0; returnValue = returnValue | (unsignedByteToInt(data[offset])); returnValue = returnValue | (unsignedByteToInt(data[offset + 1]) << 8); return StrictMath.abs(returnValue); }Java Code:public static int parseUnsigned3ByteInt(byte[] data, int offset) { int returnValue = 0; returnValue = returnValue | (unsignedByteToInt(data[offset + 2]) << 16); returnValue = returnValue | (unsignedByteToInt(data[offset + 1]) << 8); returnValue = returnValue | (unsignedByteToInt(data[offset + 0]) << 0); return StrictMath.abs(returnValue); }Thank you again, I really do appreciate your time.Java Code:public static int parseUnsigned4ByteInt(byte[] data, int offset) { int returnValue = 0; returnValue = returnValue | (unsignedByteToInt(data[offset + 3]) << 24); returnValue = returnValue | (unsignedByteToInt(data[offset + 2]) << 16); returnValue = returnValue | (unsignedByteToInt(data[offset + 1]) << 8); returnValue = returnValue | (unsignedByteToInt(data[offset + 0]) << 0); return StrictMath.abs(returnValue); }
-Kennedy
- 02-26-2010, 08:59 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,411
- Blog Entries
- 7
- Rep Power
- 17
Yep, those methods all treat the byte arrays as low endian ints. Big endian ints as stored with their 'most important' byte at a lowest address, little endian ints are stored vice versa. You may want to look a ByteBuffers, they can fiddle diddle with the endianess of ints as well.
kind regards,
Jos
Similar Threads
-
convert byte array into char array
By kgkamaraj in forum New To JavaReplies: 4Last Post: 09-13-2011, 11:32 AM -
Converting Image to byte array[] ?
By afflictedd2 in forum CLDC and MIDPReplies: 0Last Post: 04-11-2009, 11:33 PM -
String array to byte array?!
By Joe2003 in forum Advanced JavaReplies: 5Last Post: 02-28-2009, 06:09 AM -
Converting a byte[] into Sound
By savage82 in forum Advanced JavaReplies: 1Last Post: 11-21-2007, 11:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks