'not getting exact result when converting byte array to hex in java

I'm trying to get hex string value from bytebuffer using below code. Reference: In Java, how do I convert a byte array to a string of hex digits while keeping leading zeros?

    public ByteBuffer convert(ByteBuffer in) {
    StringBuffer out = new StringBuffer();
    for (int iter = 0; iter < in.array().length; iter++) {
      byte high = (byte) ( (in.array()[iter] & 0xf0) >> 4);
      byte low =  (byte)   (in.array()[iter] & 0x0f);
      out.append(nibble2char(low));
      out.append(nibble2char(high));
    }
    return ByteBuffer.wrap(out.reverse().toString().getBytes());
   }
   private static char nibble2char(byte b) {
   byte nibble = (byte) (b & 0x0f);
   if (nibble < 10) {
     return (char) ('0' + nibble);
   }
   return (char) ('a' + nibble - 10);
  }

The value I got as 701000000000, Actually I'm expecting a value like this 000000000107. So I called a reverse method on stringbuffer object at the time of wrapping.

After this I'm always getting "ffffff000000" Can someone please help me on this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source