Results 1 to 10 of 10
- 05-04-2011, 10:12 AM #1
Member
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
trouble converting unsigned int to byte array
Hi, will need some pointers on how to approach this problem.
I need to convert a series of unsigned integers (which are from file) to a byte array so i can sent this byte array out via TCP socket.
I am having problems with converting, say 999 to 3e7. This is probably due to Java not having unsigned byte conversion. This is the method I am using.
This is not all that i am facing. After converting, I am supposed to concat these byte arrays before sending them out. The problem occurs when say i convert 15 into hexadecimal F, but i need 0F instead of just F to be concated to the byte array to be sent out. Is there anyway to prepend the 0 ?Java Code:public static byte[] intToByteArray(int value, int byteSize) { byte[] b = new byte[byteSize]; for (int i = 0; i < byteSize; i++) { int offset = (b.length - 1 - i) * 8; b[i] = (byte) ((value >>> offset) & 0xFF); } return b; }
- 05-04-2011, 11:18 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Read about the ByteBuffer class: In your case you put ints in it and get bytes out of it.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-06-2011, 04:17 AM #3
Member
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
i took your advice on using the ByteBuffer class, however am not really sure about the storing and retrieving of the data contained inside.
I allocated a buffer size then try to put the following inside : 0x50, 0xff, 0xff, 0xff, 0xffJava Code:ByteBuffer tempArr = ByteBuffer.allocate(26); tempArr.asShortBuffer().put((short) 80) .put((short) 255) .put((short) 255) .put((short) 255) .put((short) 255);
Then i added another 0x0f, and try to print out the data but i got this output:Java Code:tempArr.asShortBuffer().put((short)15); tempArr.rewind(); while(tempArr.hasRemaining()){ System.out.print(Integer.toHexString(tempArr.get()) + " "); }
which is kinda strange, can someone please enlighten me?Java Code:0 f 0 ffffffff 0 ffffffff 0 ffffffff 0 ffffffff 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- 05-06-2011, 06:44 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
I don't know what you did, but I tried this quick little snippet:
... and it works fine. I didn't debug your code but I think the lengthening casts got you ;-)Java Code:import java.nio.ByteBuffer; public class T { public static void main(String[] args) { ByteBuffer tempArr = ByteBuffer.allocate(6); tempArr.asShortBuffer().put((short)0x1234).put((short)0x2345).put((short)0x3456); tempArr.rewind(); while (tempArr.remaining() > 0) System.out.printf("%02x\n", tempArr.get()); } }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-06-2011, 08:01 AM #5
Member
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
hi jos,
thanks for the assistance, can this ByteBuffer put like short, long consecutively?
my string of data is like this:
so i just invoke the ByteBuffer put method by:Java Code:data[0] = f, -> 1 byte (unsigned char) data[1] = 16, -> 2 bytes (unsigned short) data[2] = 16, -> 1 byte (unsigned char) data[3] = 3b, -> 1 byte (unsigned char) data[4] = 3b, -> 1 byte (unsigned char) data[5] = 3e7, -> 2 bytes (unsigned short) data[6] = 5a, -> 2 bytes (short) data[7] = 18fe7 -> 4 bytes (long)
Java Code:buf.asCharBuffer.put(data[0].charAt(0)); buf.asShortBuffer.put(data[1]); etc etc so is the unsigned portion being taken care off?
- 05-06-2011, 08:05 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-06-2011, 08:11 AM #7
Member
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
how about unsigned, signed data types?
- 05-06-2011, 08:27 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-06-2011, 08:45 AM #9
Member
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
roger that jos,
many thanks , much appreciated
- 05-06-2011, 09:17 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Converting unsigned long to java
By radhika.putty in forum Advanced JavaReplies: 2Last Post: 04-01-2011, 05:31 AM -
A simple question about converting byte array to unicode string
By Genom in forum New To JavaReplies: 6Last Post: 02-17-2011, 01:22 PM -
converting byte array to bmp file
By Moorkh in forum New To JavaReplies: 2Last Post: 09-07-2010, 02:58 PM -
Need help converting int to a 4 byte array
By kook04 in forum Advanced JavaReplies: 5Last Post: 02-26-2010, 08:59 PM -
Converting Image to byte array[] ?
By afflictedd2 in forum CLDC and MIDPReplies: 0Last Post: 04-11-2009, 11:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks