Results 1 to 10 of 10
  1. #1
    zsefv is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default 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.
    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;
    	}
    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 ?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Read about the ByteBuffer class: In your case you put ints in it and get bytes out of it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    zsefv is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    i took your advice on using the ByteBuffer class, however am not really sure about the storing and retrieving of the data contained inside.

    Java Code:
    ByteBuffer tempArr = ByteBuffer.allocate(26);
    tempArr.asShortBuffer().put((short) 80)
    				.put((short) 255)
    				.put((short) 255)
    				.put((short) 255)
    				.put((short) 255);
    I allocated a buffer size then try to put the following inside : 0x50, 0xff, 0xff, 0xff, 0xff

    Java Code:
    tempArr.asShortBuffer().put((short)15);
    tempArr.rewind();
    while(tempArr.hasRemaining()){
         System.out.print(Integer.toHexString(tempArr.get()) + " ");
    }
    Then i added another 0x0f, and try to print out the data but i got this output:
    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
    which is kinda strange, can someone please enlighten me?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    I don't know what you did, but I tried this quick little snippet:

    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());
    
    	}
    }
    ... and it works fine. I didn't debug your code but I think the lengthening casts got you ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    zsefv is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    hi jos,

    thanks for the assistance, can this ByteBuffer put like short, long consecutively?
    my string of data is like this:

    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)
    so i just invoke the ByteBuffer put method by:
    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?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by zsefv View Post
    hi jos,

    thanks for the assistance, can this ByteBuffer put like short, long consecutively?
    Read the API documentation for the ByteBuffer class; you can put/get all sorts of primitives (byte, short, int, long etc.) in/from the buffer.

    kind regards,

    Jos

    ps. a long takes 8 bytes, an int takes 4 bytes.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    zsefv is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    how about unsigned, signed data types?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by zsefv View Post
    how about unsigned, signed data types?
    There is no unsigned data type in Java but it doesn't matter much; just remove the sign extension wherever applicable, e.g. a byte value 0xff has its sign bits set when (implicitly) cast to an int: 0xffffffff.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    zsefv is offline Member
    Join Date
    May 2011
    Posts
    6
    Rep Power
    0

    Default

    roger that jos,

    many thanks , much appreciated

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by zsefv View Post
    roger that jos,

    many thanks , much appreciated
    You're welcome of course.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Converting unsigned long to java
    By radhika.putty in forum Advanced Java
    Replies: 2
    Last Post: 04-01-2011, 05:31 AM
  2. Replies: 6
    Last Post: 02-17-2011, 01:22 PM
  3. converting byte array to bmp file
    By Moorkh in forum New To Java
    Replies: 2
    Last Post: 09-07-2010, 02:58 PM
  4. Need help converting int to a 4 byte array
    By kook04 in forum Advanced Java
    Replies: 5
    Last Post: 02-26-2010, 08:59 PM
  5. Converting Image to byte array[] ?
    By afflictedd2 in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-11-2009, 11:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •