Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2011 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package io.undertow.util;

A utility class for mapping between byte arrays and their hex representation and back again.

Author(s):
Darran Lofthouse
 
 public class HexConverter {
 
     private static final char[] HEX_CHARS = new char[]
             {'0''1''2''3''4''5''6''7''8''9''a''b''c''d''e''f'};
 
     private static final byte[] HEX_BYTES = new byte[]
             {'0''1''2''3''4''5''6''7''8''9''a''b''c''d''e''f'};

    
Take the supplied byte array and convert it to a hex encoded String.

Parameters:
toBeConverted - the bytes to be converted.
Returns:
the hex encoded String.
 
     public static String convertToHexString(byte[] toBeConverted) {
         if (toBeConverted == null) {
             throw new NullPointerException("Parameter to be converted can not be null");
         }
 
         char[] converted = new char[toBeConverted.length * 2];
         for (int i = 0; i < toBeConverted.lengthi++) {
             byte b = toBeConverted[i];
             converted[i * 2] = [b >> 4 & 0x0F];
             converted[i * 2 + 1] = [b & 0x0F];
         }
 
         return String.valueOf(converted);
     }

    
Take the supplied byte array and convert it to to a byte array of the encoded hex values.

Each byte on the incoming array will be converted to two bytes on the return array.

Parameters:
toBeConverted - the bytes to be encoded.
Returns:
the encoded byte array.
 
     public static byte[] convertToHexBytes(byte[] toBeConverted) {
         if (toBeConverted == null) {
             throw new NullPointerException("Parameter to be converted can not be null");
         }
 
         byte[] converted = new byte[toBeConverted.length * 2];
         for (int i = 0; i < toBeConverted.lengthi++) {
             byte b = toBeConverted[i];
             converted[i * 2] = [b >> 4 & 0x0F];
             converted[i * 2 + 1] = [b & 0x0F];
         }
 
         return converted;
     }

    
Take the incoming character of hex encoded data and convert to the raw byte values.

The characters in the incoming array are processed in pairs with two chars of a pair being converted to a single byte.

Parameters:
toConvert - the hex encoded String to convert.
Returns:
the raw byte array.
 
     public static byte[] convertFromHex(final char[] toConvert) {
         if (toConvert.length % 2 != 0) {
             throw new IllegalArgumentException("The supplied character array must contain an even number of hex chars.");
         }
 
         byte[] response = new byte[toConvert.length / 2];
 
         for (int i = 0; i < response.lengthi++) {
             int posOne = i * 2;
             response[i] =   (byte)(toByte(toConvertposOne) << 4 | toByte(toConvertposOne+1));
         }
        return response;
    }
    private static byte toByte(final char[] toConvertfinal int pos) {
        int response = Character.digit(toConvert[pos], 16);
        if (response < 0 || response > 15) {
            throw new IllegalArgumentException("Non-hex character '" + toConvert[pos] + "' at index=" + pos);
        }
        return (byteresponse;
    }

    
Take the incoming String of hex encoded data and convert to the raw byte values.

The characters in the incoming String are processed in pairs with two chars of a pair being converted to a single byte.

Parameters:
toConvert - the hex encoded String to convert.
Returns:
the raw byte array.
    public static byte[] convertFromHex(final String toConvert) {
        return convertFromHex(toConvert.toCharArray());
    }
    public static void main(String[] args) {
        byte[] toConvert = new byte[256];
        for (int i = 0; i < toConvert.lengthi++) {
            toConvert[i] = (bytei;
        }
        String hexValue = convertToHexString(toConvert);
        ..println("Converted - " + hexValue);
        byte[] convertedBack = convertFromHex(hexValue);
        StringBuffer sb = new StringBuffer();
        for (byte current : convertedBack) {
            sb.append((int)current).append(" ");
        }
        ..println("Converted Back " + sb.toString());
    }
New to GrepCode? Check out our FAQ X