Class HexHelper

java.lang.Object
com.peterphi.std.util.HexHelper

public class HexHelper extends Object
A helper class which handles encoding and decoding hexidecimal
  • Method Summary

    Modifier and Type
    Method
    Description
    static final byte[]
    fromHex(char separator, String value)
    Decodes a hexidecimal string (optionally with separator characters separating each two-digit pair)
    Multiple runs of the separator will be ignored (eg.
    static final byte[]
    fromHex(String value)
    Decodes a hexidecimal string into a series of bytes
    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
    static final byte[]
    generateBytes(Random rand, int bytes)
    Generates a number of random bytes which can then be manipulated and/or converted to hex
    static final String
    generateHex(int characters)
    Generates a hexidecimal String of length characters
    static final String
    generateHex(Random random, int characters)
    Generates a hexidecimal String of length characters
    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 byte
    static 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 by separator

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • fromHex

      public static final byte[] fromHex(String value)
      Decodes a hexidecimal string into a series of bytes
      Parameters:
      value -
      Returns:
    • fromHex

      public static final byte[] fromHex(char separator, String value)
      Decodes a hexidecimal string (optionally with separator 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 character
      value -
      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

      public static final byte[] generateBytes(Random rand, int bytes)
      Generates a number of random bytes which can then be manipulated and/or converted to hex
      Parameters:
      RANDOM - the random number generator to use
      bytes -
      Returns:
    • generateHex

      public static final String generateHex(int characters)
      Generates a hexidecimal String of length characters
      Parameters:
      characters - the number of characters in the resulting String
      Returns:
    • generateHex

      public static final String generateHex(Random random, int characters)
      Generates a hexidecimal String of length characters
      Parameters:
      random - the random number generator to use
      characters - the number of characters in the resulting String
      Returns:
    • toHex

      public 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 byte
      Parameters:
      bin -
      Returns:
    • toHex

      public static 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 by separator
      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