Results 1 to 8 of 8
  1. #1
    sm123 is offline Member
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

    Default Convert 4275878552 to binary of length 4

    Hi,

    I want to convert 4275878552 to a byte array of length 4. Please help.

    I have used below code but does not work:

    buffer[0] = (byte) ((nMarkerValue >> 24) & 0xff);
    buffer[1] = (byte) ((nMarkerValue >> 16) & 0xff);
    buffer[2] = (byte) ((nMarkerValue >> 8) & 0xff);
    buffer[3] = (byte) ((nMarkerValue) & 0xff);

    Thanks,

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

    Default

    Quote Originally Posted by sm123 View Post
    Hi,

    I want to convert 4275878552 to a byte array of length 4. Please help.

    I have used below code but does not work:

    buffer[0] = (byte) ((nMarkerValue >> 24) & 0xff);
    buffer[1] = (byte) ((nMarkerValue >> 16) & 0xff);
    buffer[2] = (byte) ((nMarkerValue >> 8) & 0xff);
    buffer[3] = (byte) ((nMarkerValue) & 0xff);

    Thanks,
    Define "does not work".

    kind regards,

    Jos

  3. #3
    sm123 is offline Member
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

    Default

    Gives me wrong value.

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

    Default

    Quote Originally Posted by sm123 View Post
    Gives me wrong value.
    Well, that number in hexadecimal gives 0xfedcba98 so the four bytes should be 0xfe, 0xdc, 0xba and 0x98. What did you get?

    kind regards,

    Jos

  5. #5
    sm123 is offline Member
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

    Default

    I am getting values :

    -2
    -36
    -70
    -104

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

    Default

    Quote Originally Posted by sm123 View Post
    I am getting values :

    -2
    -36
    -70
    -104
    That is correct; bytes are also signed and are numbers in the range [-128, 127]. Add 256 to each of your numbers and see for yourself. (or make your buffer and int[]).

    kind regards,

    Jos

  7. #7
    sm123 is offline Member
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

    Default

    Thanks. It did work. I misinterpreted the bytes.

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

    Default

    Quote Originally Posted by sm123 View Post
    Thanks. It did work. I misinterpreted the bytes.
    Good; also have a look at the ByteBuffer class: it can do all that for you. If you have a byte[] already you can wrap a ByteBuffer around it or you can make it allocate it's own byte[]. You can stick ints, longs, doubles etc. in the buffer or you can read these types from it. You can also get the 'backing' byte[] out of it again. Much easier than doing it all yourself and less error prone.

    kind regards,

    Jos

Similar Threads

  1. Replies: 4
    Last Post: 10-26-2009, 11:17 PM
  2. Convert binary into decimal
    By WarmRegards in forum New To Java
    Replies: 8
    Last Post: 10-18-2009, 03:32 PM
  3. convert binary to images
    By fiqueudrue in forum New To Java
    Replies: 3
    Last Post: 02-12-2009, 10:16 AM
  4. Convert decimal to binary..pls help..newbie here
    By mephisto772 in forum New To Java
    Replies: 5
    Last Post: 02-12-2009, 09:17 AM
  5. Use recursion to convert binary to...
    By coco in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 08:46 AM

Posting Permissions

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