/*
 * BytesToHexBuffer.java
 *
 * Copyright 2011 John W Dawson
 *
 * This code is distributed under the terms of the GNU General Public License, version 3
 *
 * This class converts a byte array into a string buffer containing the
 * hexadecimal representation of the bytes
 */

import java.math.*;
public class BytesToHexBuffer
{
  static final String HEX_DIGITS = "0123456789abcdef";
  private StringBuffer buff;
  
  public BytesToHexBuffer (byte [] bytes)
  {
    // Create buffer big enough to hold the complete hex string
    buff = new StringBuffer (bytes.length * 2);
    
    // Convert each byte to a 2 digit hex representation and append to buffer
    for (int i = 0; i < bytes.length; ++i)
    {
      buff.append (HEX_DIGITS.charAt ((bytes[i] >> 4) & 0xF));
      buff.append (HEX_DIGITS.charAt (bytes[i] & 0xF));
    }
    
  }
  
  public String toString ()
  {
    return buff.toString ();
  }
}
     

