`
dadi520
  • 浏览: 140888 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

java byte array to hex String

 
阅读更多

String str = new String(byte[] byteArray);

Convert a byte array to a Hex stringTag(s):

The simple way

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;
}

A faster way

import java.io.UnsupportedEncodingException;

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) 
    throws UnsupportedEncodingException 
  {
    byte[] hex = new byte[2 * raw.length];
    int index = 0;

    for (byte b : raw) {
      int v = b & 0xFF;
      hex[index++] = HEX_CHAR_TABLE[v >>> 4];
      hex[index++] = HEX_CHAR_TABLE[v & 0xF];
    }
    return new String(hex, "ASCII");
  }

  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
     */
    
  }
}

A more elegant (based on a suggestion by Lew on usenet-cljp)

  static final String HEXES = "0123456789ABCDEF";
  public static String getHex( byte [] raw ) {
    if ( raw == null ) {
      return null;
    }
    final StringBuilder hex = new StringBuilder( 2 * raw.length );
    for ( final byte b : raw ) {
      hex.append(HEXES.charAt((b & 0xF0) >> 4))
         .append(HEXES.charAt((b & 0x0F)));
    }
    return hex.toString();
  }

<!-- HOWTO content -->

分享到:
评论

相关推荐

    DES对称分组密码系统的Java实现

    public static String bytes2Hex(byte[] data) { if (data==null) { return null; } else { int len = data.length; String str = ""; for (int i=0; i; i++) { if ((data[i]&0xFF)) str = str + "0" + ...

    VB ASP MD5 SHA HMAC AES GZIP BASE64 微信公众号EncodingAESKey 十全大补DLL

    String2Byte(dataStr As String, [ChatSet As String]) As Array(Byte) getFileBytes(fileName) As Array(Byte) 3.8 文件拆分和合并 ---------------------------------------------------------------------------...

    EmvReader Java Code

    public static String toHexString(byte[] buf) { char[] chars = new char[2 * buf.length]; for (int i = 0; i ; ++i) { chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) &gt;&gt;&gt; 4]; chars[2 * i + 1] = HEX_CHARS...

    usb communication

    'Convert the byte array to a string. DevicePathName = CStr(DetailDataBuffer()) 'Convert to Unicode. DevicePathName = StrConv(DevicePathName, vbUnicode) 'Strip cbSize (4 bytes) from the beginning....

    CE中文版-启点CE过NP中文.exe

    Added a statusbar to the hexview in memoryview Pointerscan for value scans now add the results to the overflow queue Opening a file and changing bytes do not change them to the file anymore (you need ...

    CMD以及VBS的所有命令帮助

    CByte CCur CDate CDbl Chr CInt CLng CSng CStr Hex Oct FormatCurrency FormatDateTime FormatNumber FormatPercent Abs Atn Cos Exp Hex Int Fix Log Oct Rnd Sgn Sin Sqr Tan Array Filter IsArray Join LBound ...

    python3.6.5参考手册 chm

    PEP 3101: A New Approach To String Formatting Changes To Exceptions Miscellaneous Other Changes Operators And Special Methods Builtins Build and C API Changes Performance Porting To Python 3.0 ...

    vb编程代码大全.pdf

    * HEX(num):返回十六进制值 * OCT(num):返回八进制值 * SQR(num):返回平方根 * INT(num):取整 * FIX(num):取整 * ROUND(num, n):四舍五入取小数位 * LOG(num):取以 e 为底的对数 * EXP(n):取 e 的 n 次幂 * ...

    The Art of Assembly Language Programming

    Pointers to Structures 5.7 - Sample Programs 5.7.1 - Simple Variable Declarations 5.7.2 - Using Pointer Variables 5.7.3 - Single Dimension Array Access 5.7.4 - Multidimensional Array ...

    VBSCRIPT中文手册

    String 函数 返回重复的字符串,达到指定的长度。 StrReverse 函数 返回一字符串,其中字符的顺序与指定的字符串中的顺序相反。 Sub 语句 声明形成 Sub 过程体的名称、参数和代码。 减法运算符 (-) 求两数之差,...

    vb Script参考文档

    String 函数 返回重复的字符串,达到指定的长度。 StrReverse 函数 返回一字符串,其中字符的顺序与指定的字符串中的顺序相反。 Sub 语句 声明形成 Sub 过程体的名称、参数和代码。 减法运算符 (-) 求两数之差,...

    VBScript 语言参考

    String 函数 返回重复的字符串,达到指定的长度。 StrReverse 函数 返回一字符串,其中字符的顺序与指定的字符串中的顺序相反。 Sub 语句 声明形成 Sub 过程体的名称、参数和代码。 减法运算符 (-) 求两数之差,...

    VBScript 语言参考中文手册CHM

    String 函数 返回重复的字符串,达到指定的长度。 StrReverse 函数 返回一字符串,其中字符的顺序与指定的字符串中的顺序相反。 Sub 语句 声明形成 Sub 过程体的名称、参数和代码。 减法运算符 (-) 求两数之差,...

    VBSCRIP5 -ASP用法详解

    String 函数 返回重复的字符串,达到指定的长度。 StrReverse 函数 返回一字符串,其中字符的顺序与指定的字符串中的顺序相反。 Sub 语句 声明形成 Sub 过程体的名称、参数和代码。 减法运算符 (-) 求两数之差,...

Global site tag (gtag.js) - Google Analytics