Java byte array convert to hexadecimal String via Integer helper method

Description

Java byte array convert to hexadecimal String via Integer helper method

import java.io.ByteArrayOutputStream;

public class Main {

   public static void main(String[] argv) {
      byte[] b = "demo2s.com".getBytes();
      String s = asHex(b);//from  ww w . j  a  v  a  2 s  . c om
      System.out.println(s);

      b = convert(s);

      System.out.println(new String(b));
   }
   /**
    * Returns a hexadecimal representation of the given byte array.
    *
    * @param bytes the array to output to an hex string
    * @return the hex representation as a string
    */
   public static String asHex(byte[] bytes) {
       return asHex(bytes, null);
   }

   /**
    * Returns a hexadecimal representation of the given byte array.
    *
    * @param bytes the array to output to an hex string
    * @param separator the separator to use between each byte in the output
    * string. If null no char is inserted between each byte value.
    * @return the hex representation as a string
    */
   public static String asHex(byte[] bytes, String separator) {
       StringBuilder sb = new StringBuilder();
       for (int i = 0; i < bytes.length; i++) {
           String code = Integer.toHexString(bytes[i] & 0xFF);
           if ((bytes[i] & 0xFF) < 16) {
               sb.append('0');
           }

           sb.append(code);

           if (separator != null && i < bytes.length - 1) {
               sb.append(separator);
           }
       }

       return sb.toString();
   }

   //Helper method for testing











   /**
    * Convert a String of hexadecimal digits into the corresponding byte array by
    * encoding each two hexadecimal digits as a byte.
    *
    * @param digits
    *          Hexadecimal digits representation
    *
    * @exception IllegalArgumentException
    *              if an invalid hexadecimal digit is found, or the input string
    *              contains an odd number of hexadecimal digits
    */
   public static byte[] convert(String digits) {

     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     for (int i = 0; i < digits.length(); i += 2) {
       char c1 = digits.charAt(i);
       if ((i + 1) >= digits.length())
         throw new IllegalArgumentException("hexUtil.odd");
       char c2 = digits.charAt(i + 1);
       byte b = 0;
       if ((c1 >= '0') && (c1 <= '9'))
         b += ((c1 - '0') * 16);
       else if ((c1 >= 'a') && (c1 <= 'f'))
         b += ((c1 - 'a' + 10) * 16);
       else if ((c1 >= 'A') && (c1 <= 'F'))
         b += ((c1 - 'A' + 10) * 16);
       else
         throw new IllegalArgumentException("hexUtil.bad");
       if ((c2 >= '0') && (c2 <= '9'))
         b += (c2 - '0');
       else if ((c2 >= 'a') && (c2 <= 'f'))
         b += (c2 - 'a' + 10);
       else if ((c2 >= 'A') && (c2 <= 'F'))
         b += (c2 - 'A' + 10);
       else
         throw new IllegalArgumentException("hexUtil.bad");
       baos.write(b);
     }
     return (baos.toByteArray());

   }
}



PreviousNext

Related