Top Description Fields Constructors Methods
org.apache.commons.lang3

public Class Conversion

extends Object
Class Inheritance
Imports
java.util.UUID

Static methods to convert a type into another, with endianness and bit ordering awareness.

The methods names follow a naming rule:
<source type>[source endianness][source bit ordering]To<destination type>[destination endianness][destination bit ordering]

Source/destination type fields is one of the following:

Endianness field: little-endian is the default, in this case the field is absent. In case of big-endian, the field is "Be".
Bit ordering: LSB0 is the default, in this case the field is absent. In case of MSB0, the field is "Msb0" (Camel-case).

Example: intBeMsb0ToHex convert an int with big-endian byte order and MSB0 bit order into its hexadecimal string representation

Most of the methods provide only default encoding for destination, this limits the number of ways to do one thing. Unless you are dealing with data from/to outside of the JVM platform, you should not need to use "Be" and "Msb0" methods.

Development status: work on going, only a part of the little-endian, LSB0 methods implemented so far.

Since
3.2

Field Summary

Modifier and TypeField and Description
private static final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]
private static final boolean[]

Constructor Summary

AccessConstructor and Description
public
Conversion()
Deprecated Will be removed in 4.0.0.

Constructs a new instance.

Method Summary

Modifier and TypeMethod and Description
public static char

Returns:

a hexadecimal digit representing the selected bits.
binaryBeMsb0ToHexDigit
(final boolean[]
the binary to convert.
src
)

Converts the first 4 bits of a binary (represented as boolean array) in big-endian MSB0 bit ordering to a hexadecimal digit.

public static char

Returns:

a hexadecimal digit representing the selected bits.
binaryBeMsb0ToHexDigit
(final boolean[]
the binary to convert.
src
,
final int
the position of the LSB to start the conversion.
srcPos
)

Converts a binary (represented as boolean array) in big-endian MSB0 bit ordering to a hexadecimal digit.

public static byte

Returns:

a byte containing the selected bits.
binaryToByte
(final boolean[]
the binary to convert.
src
,
final int
the position in src, in boolean unit, from where to start the conversion.
srcPos
,
final byte
initial value of the destination byte.
dstInit
,
final int
the position of the LSB, in bits, in the result byte.
dstPos
,
final int
the number of booleans to convert.
nBools
)

Converts binary (represented as boolean array) into a byte using the default (little-endian, LSB0) byte and bit ordering.

public static char

Returns:

a hexadecimal digit representing the selected bits.
binaryToHexDigit
(final boolean[]
the binary to convert.
src
)

Converts binary (represented as boolean array) to a hexadecimal digit using the default (LSB0) bit ordering.

public static char

Returns:

a hexadecimal digit representing the selected bits.
binaryToHexDigit
(final boolean[]
the binary to convert.
src
,
final int
the position of the LSB to start the conversion.
srcPos
)

Converts binary (represented as boolean array) to a hexadecimal digit using the default (LSB0) bit ordering.

public static char

Returns:

a hexadecimal digit representing the selected bits.
binaryToHexDigitMsb0_4bits
(final boolean[]
the binary to convert.
src
)

Converts binary (represented as boolean array) to a hexadecimal digit using the MSB0 bit ordering.

public static char

Returns:

a hexadecimal digit representing the selected bits.
binaryToHexDigitMsb0_4bits
(final boolean[]
the binary to convert.
src
,
final int
the position of the LSB to start the conversion.
srcPos
)

Converts binary (represented as boolean array) to a hexadecimal digit using the MSB0 bit ordering.

public static int

Returns:

an int containing the selected bits.
binaryToInt
(final boolean[]
the binary to convert.
src
,
final int
the position in src, in boolean unit, from where to start the conversion.
srcPos
,
final int
initial value of the destination int.
dstInit
,
final int
the position of the LSB, in bits, in the result int.
dstPos
,
final int
the number of booleans to convert.
nBools
)

Converts binary (represented as boolean array) into an int using the default (little endian, LSB0) byte and bit ordering.

public static long

Returns:

a long containing the selected bits.
binaryToLong
(final boolean[]
the binary to convert.
src
,
final int
the position in src, in boolean unit, from where to start the conversion.
srcPos
,
final long
initial value of the destination long.
dstInit
,
final int
the position of the LSB, in bits, in the result long.
dstPos
,
final int
the number of booleans to convert.
nBools
)

Converts binary (represented as boolean array) into a long using the default (little endian, LSB0) byte and bit ordering.

public static short

Returns:

a short containing the selected bits.
binaryToShort
(final boolean[]
the binary to convert.
src
,
final int
the position in src, in boolean unit, from where to start the conversion.
srcPos
,
final short
initial value of the destination short.
dstInit
,
final int
the position of the LSB, in bits, in the result short.
dstPos
,
final int
the number of booleans to convert.
nBools
)

Converts binary (represented as boolean array) into a short using the default (little endian, LSB0) byte and bit ordering.

public static int

Returns:

an int containing the selected bits.
byteArrayToInt
(final byte[]
the byte array to convert.
src
,
final int
the position in src, in byte unit, from where to start the conversion.
srcPos
,
final int
initial value of the destination int.
dstInit
,
final int
the position of the LSB, in bits, in the result int.
dstPos
,
final int
the number of bytes to convert.
nBytes
)

Converts an array of byte into an int using the default (little-endian, LSB0) byte and bit ordering.

public static long

Returns:

a long containing the selected bits.
byteArrayToLong
(final byte[]
the byte array to convert.
src
,
final int
the position in src, in byte unit, from where to start the conversion.
srcPos
,
final long
initial value of the destination long.
dstInit
,
final int
the position of the LSB, in bits, in the result long.
dstPos
,
final int
the number of bytes to convert.
nBytes
)

Converts an array of byte into a long using the default (little-endian, LSB0) byte and bit ordering.

public static short

Returns:

a short containing the selected bits.
byteArrayToShort
(final byte[]
the byte array to convert.
src
,
final int
the position in src, in byte unit, from where to start the conversion.
srcPos
,
final short
initial value of the destination short.
dstInit
,
final int
the position of the LSB, in bits, in the result short.
dstPos
,
final int
the number of bytes to convert.
nBytes
)

Converts an array of byte into a short using the default (little-endian, LSB0) byte and bit ordering.

public static UUID

Returns:

a UUID.
byteArrayToUuid
(final byte[]
the byte array to convert.
src
,
final int
the position in src where to copy the result from.
srcPos
)

Converts bytes from an array into a UUID using the default (little-endian, LSB0) byte and bit ordering.

public static boolean[]

Returns:

dst.
byteToBinary
(final byte
the byte to convert.
src
,
final int
the position in src, in bits, from where to start the conversion.
srcPos
,
final boolean[]
the destination array.
dst
,
final int
the position in dst where to copy the result.
dstPos
,
final int
the number of booleans to copy to dst, must be smaller or equal to the width of the input (from srcPos to MSB).
nBools
)

Converts a byte into an array of boolean using the default (little-endian, LSB0) byte and bit ordering.

public static String

Returns:

dst.
byteToHex
(final byte
the byte to convert.
src
,
final int
the position in src, in bits, from where to start the conversion.
srcPos
,
final String
the initial value for the result String.
dstInit
,
final int
the position in dst where to copy the result.
dstPos
,
final int
the number of chars to copy to dst, must be smaller or equal to the width of the input (from srcPos to MSB).
nHexs
)

Converts a byte into an array of char using the default (little-endian, LSB0) byte and bit ordering.

public static boolean[]

Returns:

a boolean array with the binary representation of hexDigit.
hexDigitMsb0ToBinary
(final char
the hexadecimal digit to convert.
hexChar
)

Converts a hexadecimal digit into binary (represented as boolean array) using the MSB0 bit ordering.

public static int

Returns:

an int equals to hexDigit.
hexDigitMsb0ToInt
(final char
the hexadecimal digit to convert.
hexChar
)

Converts a hexadecimal digit into an int using the MSB0 bit ordering.

public static boolean[]

Returns:

a boolean array with the binary representation of hexDigit.
hexDigitToBinary
(final char
the hexadecimal digit to convert.
hexChar
)

Converts a hexadecimal digit into binary (represented as boolean array) using the default (LSB0) bit ordering.

public static int

Returns:

an int equals to hexDigit.
hexDigitToInt
(final char
the hexadecimal digit to convert.
hexChar
)

Converts a hexadecimal digit into an int using the default (LSB0) bit ordering.

public static byte

Returns:

a byte containing the selected bits.
hexToByte
(final String
the hexadecimal string to convert.
src
,
final int
the position in src, in char unit, from where to start the conversion.
srcPos
,
final byte
initial value of the destination byte.
dstInit
,
final int
the position of the LSB, in bits, in the result byte.
dstPos
,
final int
the number of Chars to convert.
nHex
)

Converts a hexadecimal string into a byte using the default (little-endian, LSB0) byte and bit ordering.

public static int

Returns:

an int containing the selected bits.
hexToInt
(final String
the hexadecimal string to convert.
src
,
final int
the position in src, in char unit, from where to start the conversion.
srcPos
,
final int
initial value of the destination int.
dstInit
,
final int
the position of the LSB, in bits, in the result int.
dstPos
,
final int
the number of chars to convert.
nHex
)

Converts an array of char into an int using the default (little-endian, LSB0) byte and bit ordering.

public static long

Returns:

a long containing the selected bits.
hexToLong
(final String
the hexadecimal string to convert.
src
,
final int
the position in src, in char unit, from where to start the conversion.
srcPos
,
final long
initial value of the destination long.
dstInit
,
final int
the position of the LSB, in bits, in the result long.
dstPos
,
final int
the number of chars to convert.
nHex
)

Converts an array of char into a long using the default (little-endian, LSB0) byte and bit ordering.

public static short

Returns:

a short containing the selected bits.
hexToShort
(final String
the hexadecimal string to convert.
src
,
final int
the position in src, in char unit, from where to start the conversion.
srcPos
,
final short
initial value of the destination short.
dstInit
,
final int
the position of the LSB, in bits, in the result short.
dstPos
,
final int
the number of chars to convert.
nHex
)

Converts an array of char into a short using the default (little-endian, LSB0) byte and bit ordering.

public static long

Returns:

a long containing the selected bits.
intArrayToLong
(final int[]
the int array to convert.
src
,
final int
the position in src, in int unit, from where to start the conversion.
srcPos
,
final long
initial value of the destination long.
dstInit
,
final int
the position of the LSB, in bits, in the result long.
dstPos
,
final int
the number of ints to convert.
nInts
)

Converts an array of int into a long using the default (little-endian, LSB0) byte and bit ordering.

public static boolean[]

Returns:

dst.
intToBinary
(final int
the int to convert.
src
,
final int
the position in src, in bits, from where to start the conversion.
srcPos
,
final boolean[]
the destination array.
dst
,
final int
the position in dst where to copy the result.
dstPos
,
final int
the number of booleans to copy to dst, must be smaller or equal to the width of the input (from srcPos to MSB).
nBools
)

Converts an int into an array of boolean using the default (little-endian, LSB0) byte and bit ordering.

public static byte[]

Returns:

dst.
intToByteArray
(final int
the int to convert.
src
,
final int
the position in src, in bits, from where to start the conversion.
srcPos
,
final byte[]
the destination array.
dst
,
final int
the position in dst where to copy the result.
dstPos
,
final int
the number of bytes to copy to dst, must be smaller or equal to the width of the input (from srcPos to MSB).
nBytes
)

Converts an int into an array of byte using the default (little-endian, LSB0) byte and bit ordering.

public static String

Returns:

dst.
intToHex
(final int
the int to convert.
src
,
final int
the position in src, in bits, from where to start the conversion.
srcPos
,
final String
the initial value for the result String.
dstInit
,
final int
the position in dst where to copy the result.
dstPos
,
final int
the number of chars to copy to dst, must be smaller or equal to the width of the input (from srcPos to MSB).
nHexs
)

Converts an int into an array of char using the default (little-endian, LSB0) byte and bit ordering.

public static char

Returns:

a hexadecimal digit representing the 4 LSB of nibble.
intToHexDigit
(final int
the 4 bits to convert.
nibble
)

Converts the 4 LSB of an int to a hexadecimal digit.

public static char

Returns:

a hexadecimal digit representing the 4 LSB of nibble.
intToHexDigitMsb0
(final int
the 4 bits to convert.
nibble
)

Converts the 4 LSB of an int to a hexadecimal digit encoded using the MSB0 bit ordering.

public static short[]

Returns:

dst.
intToShortArray
(final int
the int to convert.
src
,
final int
the position in src, in bits, from where to start the conversion.
srcPos
,
final short[]
the destination array.
dst
,
final int
the position in dst where to copy the result.
dstPos
,
final int
the number of shorts to copy to dst, must be smaller or equal to the width of the input (from srcPos to MSB).
nShorts
)

Converts an int into an array of short using the default (little-endian, LSB0) byte and bit ordering.

public static boolean[]

Returns:

dst.
longToBinary
(final long
the long to convert.
src
,
final int
the position in src, in bits, from where to start the conversion.
srcPos
,
final boolean[]
the destination array.
dst
,
final int
the position in dst where to copy the result.
dstPos
,
final int
the number of booleans to copy to dst, must be smaller or equal to the width of the input (from srcPos to MSB).
nBools
)

Converts a long into an array of boolean using the default (little-endian, LSB0) byte and bit ordering.

public static byte[]

Returns:

dst.
longToByteArray
(final long
the long to convert.
src
,
final int
the position in src, in bits, from where to start the conversion.
srcPos
,
final byte[]
the destination array.
dst
,
final int
the position in dst where to copy the result.
dstPos
,
final int
the number of bytes to copy to dst, must be smaller or equal to the width of the input (from srcPos to MSB).
nBytes
)

Converts a long into an array of byte using the default (little-endian, LSB0) byte and bit ordering.

public static String

Returns:

dst.
longToHex
(final long
the long to convert.
src
,
final int
the position in src, in bits, from where to start the conversion.
srcPos
,
final String
the initial value for the result String.
dstInit
,
final int
the position in dst where to copy the result.
dstPos
,
final int
the number of chars to copy to dst, must be smaller or equal to the width of the input (from srcPos to MSB).
nHexs
)

Converts a long into an array of char using the default (little-endian, LSB0) byte and bit ordering.

public static int[]

Returns:

dst.
longToIntArray
(final long
the long to convert.
src
,
final int
the position in src, in bits, from where to start the conversion.
srcPos
,
final int[]
the destination array.
dst
,
final int
the position in dst where to copy the result.
dstPos
,
final int
the number of ints to copy to dst, must be smaller or equal to the width of the input (from srcPos to MSB).
nInts
)

Converts a long into an array of int using the default (little-endian, LSB0) byte and bit ordering.

public static short[]

Returns:

dst.
longToShortArray
(final long
the long to convert.
src
,
final int
the position in src, in bits, from where to start the conversion.
srcPos
,
final short[]
the destination array.
dst
,
final int
the position in dst where to copy the result.
dstPos
,
final int
the number of shorts to copy to dst, must be smaller or equal to the width of the input (from srcPos to MSB).
nShorts
)

Converts a long into an array of short using the default (little-endian, LSB0) byte and bit ordering.

public static int

Returns:

an int containing the selected bits.
shortArrayToInt
(final short[]
the short array to convert.
src
,
final int
the position in src, in short unit, from where to start the conversion.
srcPos
,
final int
initial value of the destination int.
dstInit
,
final int
the position of the LSB, in bits, in the result int.
dstPos
,
final int
the number of shorts to convert.
nShorts
)

Converts an array of short into an int using the default (little-endian, LSB0) byte and bit ordering.

public static long

Returns:

a long containing the selected bits.
shortArrayToLong
(final short[]
the short array to convert.
src
,
final int
the position in src, in short unit, from where to start the conversion.
srcPos
,
final long
initial value of the destination long.
dstInit
,
final int
the position of the LSB, in bits, in the result long.
dstPos
,
final int
the number of shorts to convert.
nShorts
)

Converts an array of short into a long using the default (little-endian, LSB0) byte and bit ordering.

public static boolean[]

Returns:

dst.
shortToBinary
(final short
the short to convert.
src
,
final int
the position in src, in bits, from where to start the conversion.
srcPos
,
final boolean[]
the destination array.
dst
,
final int
the position in dst where to copy the result.
dstPos
,
final int
the number of booleans to copy to dst, must be smaller or equal to the width of the input (from srcPos to MSB).
nBools
)

Converts a short into an array of boolean using the default (little-endian, LSB0) byte and bit ordering.

public static byte[]

Returns:

dst.
shortToByteArray
(final short
the short to convert.
src
,
final int
the position in src, in bits, from where to start the conversion.
srcPos
,
final byte[]
the destination array.
dst
,
final int
the position in dst where to copy the result.
dstPos
,
final int
the number of bytes to copy to dst, must be smaller or equal to the width of the input (from srcPos to MSB).
nBytes
)

Converts a short into an array of byte using the default (little-endian, LSB0) byte and bit ordering.

public static String

Returns:

dst.
shortToHex
(final short
the short to convert.
src
,
final int
the position in src, in bits, from where to start the conversion.
srcPos
,
final String
the initial value for the result String.
dstInit
,
final int
the position in dst where to copy the result.
dstPos
,
final int
the number of chars to copy to dst, must be smaller or equal to the width of the input (from srcPos to MSB).
nHexs
)

Converts a short into an array of char using the default (little-endian, LSB0) byte and bit ordering.

public static byte[]

Returns:

dst.
uuidToByteArray
(final UUID
the UUID to convert.
src
,
final byte[]
the destination array.
dst
,
final int
the position in dst where to copy the result.
dstPos
,
final int
the number of bytes to copy to dst, must be smaller or equal to the width of the input (from srcPos to MSB).
nBytes
)

Converts UUID into an array of byte using the default (little-endian, LSB0) byte and bit ordering.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait