Package com.peterphi.std.util
Class HexHelper
java.lang.Object
com.peterphi.std.util.HexHelper
A helper class which handles encoding and decoding hexidecimal
-
Method Summary
Modifier and TypeMethodDescriptionstatic final byte[]
Decodes a hexidecimal string (optionally withseparator
characters separating each two-digit pair)
Multiple runs of the separator will be ignored (eg.static final byte[]
Decodes a hexidecimal string into a series of bytesstatic final byte[]
generateBytes
(int bytes) Generates a number of random bytes which can then be manipulated and/or converted to hex
Uses a new instance of java.util.Randomstatic final byte[]
generateBytes
(Random rand, int bytes) Generates a number of random bytes which can then be manipulated and/or converted to hexstatic final String
generateHex
(int characters) Generates a hexidecimal String of lengthcharacters
static final String
generateHex
(Random random, int characters) Generates a hexidecimal String of lengthcharacters
static final String
toHex
(byte... bin) Encodes a series of bytes into a hexidecimal string (potentially with leading zeroes) with no separators between each source bytestatic final String
toHex
(char separator, byte... bin) Encodes a series of bytes into a hexidecimal string with each source byte (represented in the output as a 2 digit hexidecimal pair) separated byseparator
-
Method Details
-
fromHex
Decodes a hexidecimal string into a series of bytes- Parameters:
value
-- Returns:
-
fromHex
Decodes a hexidecimal string (optionally withseparator
characters separating each two-digit pair)
Multiple runs of the separator will be ignored (eg. AA::BB will result in[0xAA,0xBB]
, not[0xAA,0x00,0xBB]
)- Parameters:
separator
- the separator charactervalue
-- Returns:
-
generateBytes
public static final byte[] generateBytes(int bytes) Generates a number of random bytes which can then be manipulated and/or converted to hex
Uses a new instance of java.util.Random- Parameters:
bytes
-- Returns:
-
generateBytes
Generates a number of random bytes which can then be manipulated and/or converted to hex- Parameters:
RANDOM
- the random number generator to usebytes
-- Returns:
-
generateHex
Generates a hexidecimal String of lengthcharacters
- Parameters:
characters
- the number of characters in the resulting String- Returns:
-
generateHex
Generates a hexidecimal String of lengthcharacters
- Parameters:
random
- the random number generator to usecharacters
- the number of characters in the resulting String- Returns:
-
toHex
Encodes a series of bytes into a hexidecimal string (potentially with leading zeroes) with no separators between each source byte- Parameters:
bin
-- Returns:
-
toHex
Encodes a series of bytes into a hexidecimal string with each source byte (represented in the output as a 2 digit hexidecimal pair) separated byseparator
- Parameters:
separator
- The character to insert between each byte (for example,':'
)bin
- the series of bytes to encode- Returns:
- a hexidecimal string with each source byte (represented in the output as a 2 digit hexidecimal pair) separated by
separator
-