layout: tutorial title: Extracting values description: Extracting values group: nav-right categories: [“bytes”] previous: manipulating-bytes.md next: mutable-bytes.md

You can extract values from a bytes object into native Java objects such as ints and longs, bytes, byte arrays and so on.

Note all the methods here take an optional ByteOrder argument, defaulting to big endian by default.

toInt() and toLong()

The method toInt() and the method toLong() respectively translate the bytes values into an int or a long, requiring respectively the value to be at most 4 or 8 bytes long.

get(i)

The get(i) method provides the byte at index i.

getInt(i) and getLong(i)

The method getInt() and the method getLong() respectively return the next 4 or 8 bytes into an int or a long.

toArray() and toArrayUnsafe()

The method toArray copies the bytes of the object into a new bytes array.

The method toArrayUnsafe makes available the underlying byte array of the object - modifying it changes the Bytes object. Note this is more performant as it doesn't allocate new memory.

To BigIntegers

The method toUnsignedBigInteger creates a new unsigned BigInteger object with the contents of the Bytes object. You can also use the method toBigInteger to represent Bytes as a signed integer, using the two's-complement representation.

Transforming Bytes into strings

There is a sleuth of options to turn bytes into strings, and they all have different use cases.

  • The method toHexString provides the value represented as hexadecimal, starting with “0x”.
  • The method toUnprefixedHexString provides the value represented as hexadecimal, no “0x” prefix though.
  • The method toShortHexString provides the value represented as a minimal hexadecimal string (without any leading zero).
  • The method toQuantityHexString provides the value represented as a minimal hexadecimal string (without any leading zero, except if it's valued zero or empty, in which case it returns 0x0).
  • The method toEllipsisHexString provides the first 3 bytes and last 3 bytes represented as hexadecimal strings, joined with an ellipsis (...).

By default, toString() calls toHexString().