Difference between revisions of "Convert a byte array to a Hex string"

From CodeCodex

(Java)
 
(Java)
Line 1: Line 1:
 
==Implementations==  
 
==Implementations==  
 
===Java===
 
===Java===
 +
JDK 1.5
 +
<highlightsyntax language="java122">
 +
public static String getHexString(byte[] b) throws Exception {
 +
  String result = "";
 +
  for (int i=0; i < b.length; i++) {
 +
    result += String.format("%02x", b[i] & 0xff);
 +
  }
 +
  return result;
 +
}
 +
</highlightsyntax>
 
The simple way  
 
The simple way  
 
<highlightsyntax language="java122">
 
<highlightsyntax language="java122">
Line 17: Line 27:
  
 
public class StringUtils {
 
public class StringUtils {
   
 
  static final byte[] HEX_CHAR_TABLE = {
 
    (byte)'0', (byte)'1', (byte)'2', (byte)'3',
 
    (byte)'4', (byte)'5', (byte)'6', (byte)'7',
 
    (byte)'8', (byte)'9', (byte)'a', (byte)'b',
 
    (byte)'c', (byte)'d', (byte)'e', (byte)'f'
 
  };   
 
  
 
   public static String getHexString(byte[] raw)  
 
   public static String getHexString(byte[] raw)  
 
     throws UnsupportedEncodingException  
 
     throws UnsupportedEncodingException  
 
   {
 
   {
     byte[] hex = new byte[2 * raw.length];
+
     char[] hex = new char[2 * raw.length];
 
     int index = 0;
 
     int index = 0;
  
 
     for (byte b : raw) {
 
     for (byte b : raw) {
 
       int v = b & 0xFF;
 
       int v = b & 0xFF;
       hex[index++] = HEX_CHAR_TABLE[v >>> 4];
+
       hex[index++] = Character.forDigit(v >>> 4, 16);
       hex[index++] = HEX_CHAR_TABLE[v & 0xF];
+
       hex[index++] = Character.forDigit(v & 0xF, 16);
 
     }
 
     }
     return new String(hex, "ASCII");
+
     return new String(hex);
 
   }
 
   }
  

Revision as of 05:07, 18 August 2009

Implementations

Java

JDK 1.5 <highlightsyntax language="java122">

public static String getHexString(byte[] b) throws Exception {
 String result = "";
 for (int i=0; i < b.length; i++) {
   result += String.format("%02x", b[i] & 0xff);
 }
 return result;

} </highlightsyntax> The simple way <highlightsyntax language="java122">

public static String getHexString(byte[] b) throws Exception {
 String result = "";
 for (int i=0; i < b.length; i++) {
   result +=
         Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
 }
 return result;

} </highlightsyntax> A faster way <highlightsyntax language="java122">

import java.io.UnsupportedEncodingException;

public class StringUtils {

 public static String getHexString(byte[] raw) 
   throws UnsupportedEncodingException 
 {
   char[] hex = new char[2 * raw.length];
   int index = 0;
   for (byte b : raw) {
     int v = b & 0xFF;
     hex[index++] = Character.forDigit(v >>> 4, 16);
     hex[index++] = Character.forDigit(v & 0xF, 16);
   }
   return new String(hex);
 }
 public static void main(String args[]) throws Exception{
   byte[] byteArray = {
     (byte)255, (byte)254, (byte)253, 
     (byte)252, (byte)251, (byte)250
   };
   System.out.println(StringUtils.getHexString(byteArray));
   
   /*
    * output :
    *   fffefdfcfbfa
    */
   
 }

} </highlightsyntax>