Class Hex


  • public final class Hex
    extends java.lang.Object
    Utility functions for hex encoding.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static byte[] HEX_BYTES
      for hex conversion.
      private static char[] HEX_CHARS  
      private static org.apache.commons.logging.Log LOG  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Hex()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] decodeBase64​(java.lang.String base64Value)
      Decode a base64 String.
      static byte[] decodeHex​(java.lang.String s)
      Decodes a hex String into a byte array.
      static byte[] getBytes​(byte b)
      Returns the bytes corresponding to the ASCII hex encoding of the given byte.
      static byte[] getBytes​(byte[] bytes)
      Returns the bytes corresponding to the ASCII hex encoding of the given bytes.
      static char[] getChars​(short num)
      Returns the characters corresponding to the ASCII hex encoding of the given short.
      static char[] getCharsUTF16BE​(java.lang.String text)
      Takes the characters in the given string, convert it to bytes in UTF16-BE format and build a char array that corresponds to the ASCII hex encoding of the resulting bytes.
      private static int getHighNibble​(byte b)
      Get the high nibble of the given byte.
      private static int getLowNibble​(byte b)
      Get the low nibble of the given byte.
      static java.lang.String getString​(byte b)
      Returns a hex string of the given byte.
      static java.lang.String getString​(byte[] bytes)
      Returns a hex string of the given byte array.
      static void writeHexByte​(byte b, java.io.OutputStream output)
      Writes the given byte as hex value to the given output stream.
      static void writeHexBytes​(byte[] bytes, java.io.OutputStream output)
      Writes the given byte array as hex value to the given output stream.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LOG

        private static final org.apache.commons.logging.Log LOG
      • HEX_BYTES

        private static final byte[] HEX_BYTES
        for hex conversion. https://stackoverflow.com/questions/2817752/java-code-to-convert-byte-to-hexadecimal
      • HEX_CHARS

        private static final char[] HEX_CHARS
    • Constructor Detail

      • Hex

        private Hex()
    • Method Detail

      • getString

        public static java.lang.String getString​(byte b)
        Returns a hex string of the given byte.
      • getString

        public static java.lang.String getString​(byte[] bytes)
        Returns a hex string of the given byte array.
      • getBytes

        public static byte[] getBytes​(byte b)
        Returns the bytes corresponding to the ASCII hex encoding of the given byte.
      • getBytes

        public static byte[] getBytes​(byte[] bytes)
        Returns the bytes corresponding to the ASCII hex encoding of the given bytes.
      • getChars

        public static char[] getChars​(short num)
        Returns the characters corresponding to the ASCII hex encoding of the given short.
      • getCharsUTF16BE

        public static char[] getCharsUTF16BE​(java.lang.String text)
        Takes the characters in the given string, convert it to bytes in UTF16-BE format and build a char array that corresponds to the ASCII hex encoding of the resulting bytes. Example:
           getCharsUTF16BE("ab") == new char[]{'0','0','6','1','0','0','6','2'}
         
        Parameters:
        text - The string to convert
        Returns:
        The string converted to hex
      • writeHexByte

        public static void writeHexByte​(byte b,
                                        java.io.OutputStream output)
                                 throws java.io.IOException
        Writes the given byte as hex value to the given output stream.
        Parameters:
        b - the byte to be written
        output - the output stream to be written to
        Throws:
        java.io.IOException - exception if anything went wrong
      • writeHexBytes

        public static void writeHexBytes​(byte[] bytes,
                                         java.io.OutputStream output)
                                  throws java.io.IOException
        Writes the given byte array as hex value to the given output stream.
        Parameters:
        bytes - the byte array to be written
        output - the output stream to be written to
        Throws:
        java.io.IOException - exception if anything went wrong
      • getHighNibble

        private static int getHighNibble​(byte b)
        Get the high nibble of the given byte.
        Parameters:
        b - the given byte
        Returns:
        the high nibble
      • getLowNibble

        private static int getLowNibble​(byte b)
        Get the low nibble of the given byte.
        Parameters:
        b - the given byte
        Returns:
        the low nibble
      • decodeBase64

        public static byte[] decodeBase64​(java.lang.String base64Value)
        Decode a base64 String.
        Parameters:
        base64Value - a base64 encoded String.
        Returns:
        the decoded String as a byte array.
        Throws:
        java.lang.IllegalArgumentException - if this isn't a base64 encoded string.
      • decodeHex

        public static byte[] decodeHex​(java.lang.String s)
                                throws java.io.IOException
        Decodes a hex String into a byte array.
        Parameters:
        s - A String with ASCII hex.
        Returns:
        decoded byte array.
        Throws:
        java.io.IOException