This class provides methods for working with hexadecimal representations of data. : Hexadecimal « Data Type « Java






This class provides methods for working with hexadecimal representations of data.

       
/*
 * JSwiff is an open source Java API for Macromedia Flash file generation
 * and manipulation
 *
 * Copyright (C) 2004-2005 Ralf Terdic (contact@jswiff.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

//package com.jswiff.util;

/**
 * This class provides methods for working with hexadecimal representations of data.
 */
public class HexUtils {
  private static final char[] HEX_DIGITS = {
      '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
      'E', 'F'
    };

  /**
   * Returns the hexadecimal representation of a byte array.
   * 
   * @param data byte array
   *
   * @return hex representation
   */
  public static String toHex(final byte[] data) {
    return toHex(data, 0, data.length);
  }

  /**
   * Returns the hexadecimal representation of a part of a byte array.
   *
   * @param data byte array
   * @param startPos start position
   * @param length length of the relevant array portion
   *
   * @return hex representation
   */
  public static String toHex(
    final byte[] data, final int startPos, final int length) {
    StringBuffer b = new StringBuffer();
    int endPos     = startPos + length;
    for (int i = startPos; i < endPos; i++) {
      if (i > 0) {
        b.append(' ');
      }
      int c = data[i];
      b.append(HEX_DIGITS[(c & 0xF0) >> 4]);
      b.append(HEX_DIGITS[(c & 0x0F) >> 0]);
    }
    return b.toString();
  }
}

   
    
    
    
    
    
    
  








Related examples in the same category

1.Convert Decimal to Hexadecimal
2.Convert from decimal to hexadecimal
3.Convert from Byte array to hexadecimal string
4.Convert from decimal to hexadecimal with leading zeroes and uppercase
5.Hex encoder and decoder.
6.Decode hex string to a byte array
7.Returns the hexadecimal value of the supplied byte array
8.Convert byte array to Hex String
9.Convert the bytes to a hex string representation of the bytes
10.Hex decimal dump
11.hex decoder
12.Get Base64 From HEX
13.Converts a hex string to a byte array.
14.append hex digit
15.dump an array of bytes in hex form
16.Get byte array from hex string
17.Check if the current character is an Hex Char <hex> ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66]
18.Helper function that returns a char from an hex
19.Hex encoder/decoder implementation borrowed from BouncyCastle
20.Java Hex dump Library
21.Util for digesting and encoding bytes/strings.
22.Provide hex utility for converting bytes to hex string
23.Number in hexadecimal format are used throughout Freenet.
24.Convert to/from Hex string
25.This class allows a number to be easily formatted as a hexadecimal number. The representation uses 0-f.
26.Hex Util