converting int and short to byte array and vice versa

Discussion in 'Java' started by carmen, Apr 13, 2005.

  1. carmen

    carmen Guest

    Hi,

    Could anyone suggest a way to convert a short to a 2 byte array and a
    int to a 4 byte array and vice versa? Is there any way to do it using
    utility classes provided by java?
    I have just begun to code in java :(,

    regards,
    Carmen
    carmen, Apr 13, 2005
    #1
    1. Advertising

  2. carmen

    foo Guest

    // For int. for short is similar

    /**
    * Returns rightmost byte as the 0th element of the array
    */
    bytesValue(int myInt) {
    byte[] bytes = new byte[4];
    hexBase = 0xff; // A byte of all ones
    bytes[0] = hexBase & myInt;
    bytes[1] = ((hexBase << 8) & myInt) >> 8;
    bytes[2] = ((hexBase << 16) & myInt) >> 16;
    bytes[3] = ((hexBase << 24) & myInt) >> 24;
    return byte;
    }

    intValue(byte[] bytes) {
    int myInt = bytes[3];
    myInt = (myInt << 8) | bytes[2];
    myInt = (myInt << 8) | bytes[1];
    myInt = (myInt << 8) | bytes[0];
    return myInt;
    }
    foo, Apr 13, 2005
    #2
    1. Advertising

  3. carmen wrote:
    > Hi,
    >
    > Could anyone suggest a way to convert a short to a 2 byte array and a
    > int to a 4 byte array and vice versa? Is there any way to do it using
    > utility classes provided by java?
    > I have just begun to code in java :(,
    >
    > regards,
    > Carmen
    >


    The inbuilt utilities are probably to wrap a DataOutputStream
    round a ByteArrayOutputStream, and DataInputStream round a
    ByteArrayInputStream, but foo's bit-shifting ways are probably
    quicker and easier.

    Steve
    Steve Horsley, Apr 13, 2005
    #3
  4. carmen

    erikstenqvist

    Joined:
    Jan 13, 2009
    Messages:
    1
    Hi,
    I was looking for the answer to the same question (with shorts ) and got a hint from foo's answer, but didn't get it to work. So I decided to learn the basics about bits to solve it. I had a lot of help from the Campfire story "Cat and Mouse Games with Bits" at the site "JavaRanch".
    Then I found my own solution of the problem, and then i found that it was almost identical with foo's. :)
    I think that the reason why foo's code didn't work for me was (not only that 8 followed by ) looks like a 8) ) that when you add the bytes bitwise java treats them like integers and fills remaining bytes with ones or zeros depending on sign. In foo's code it can probably be solved by adding the "hexBase" to the second part. Turning this:
    to something similar to this:
    ...
    myInt = (myInt << 8 ) | (bytes[2] & hexBase ) ;
    ...
    At least my code is working now.
    Thank you all!
    Erik
    erikstenqvist, Jan 13, 2009
    #4
  5. carmen

    ersecchio

    Joined:
    Jan 12, 2010
    Messages:
    1
    I have an array of byte:
    byte number = new byte[2];

    all numerical valure int array "number" are expressend as multi-byte integer.
    I would convert the numer into array "number" as int (integer).

    how made?
    ersecchio, Jan 12, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Astan Chee
    Replies:
    1
    Views:
    367
    Paul McGuire
    Mar 13, 2007
  2. Amit Khemka
    Replies:
    1
    Views:
    1,426
    Paul Boddie
    Mar 13, 2007
  3. FPGA
    Replies:
    5
    Views:
    1,258
    Thomas Stanka
    Jan 8, 2008
  4. Eugene Anthony

    Byte string to Unicode & vice versa

    Eugene Anthony, Apr 27, 2006, in forum: ASP General
    Replies:
    1
    Views:
    149
    Eugene Anthony
    Apr 28, 2006
  5. chen li
    Replies:
    3
    Views:
    106
    Daniel Martin
    Jul 13, 2007
Loading...

Share This Page