zoukankan      html  css  js  c++  java
  • zZ Java中String和Byte[]之间的那些事

    Java中String和byte[]数组之间的转换很多,转载一些代码作为日后使用参考。

    转载自:[http://hongyiqiye.iteye.com/blog/1028687]

    public static String stringToHexString(String strPart) {
            String hexString = "";
            for (int i = 0; i < strPart.length(); i++) {
                int ch = (int) strPart.charAt(i);
                String strHex = Integer.toHexString(ch);
                hexString = hexString + strHex;
            }
            return hexString;
        }

    private static String hexString="0123456789ABCDEF";

    public static String encode(String str)
    {
    // 根据默认编码获取字节数组
    byte[] bytes=str.getBytes();
    StringBuilder sb=new StringBuilder(bytes.length*2);
    // 将字节数组中每个字节拆解成2位16进制整数
    for(int i=0;i<bytes.length;i++)
    {
    sb.append(hexString.charAt((bytes[i]&0xf0)>>4));
    sb.append(hexString.charAt((bytes[i]&0x0f)>>0));
    }
    return sb.toString();
    }

    public static String decode(String bytes)
    {
    ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2);
    // 将每2位16进制整数组装成一个字节
    for(int i=0;i<bytes.length();i+=2)
    baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1))));
    return new String(baos.toByteArray());
    }

    private static byte uniteBytes(byte src0, byte src1) {
         byte _b0 = Byte.decode("0x" + new String(new byte[] {src0})).byteValue();
         _b0 = (byte) (_b0 << 4);
         byte _b1 = Byte.decode("0x" + new String(new byte[] {src1})).byteValue();
         byte ret = (byte) (_b0 | _b1);
         return ret;

    public static byte[] HexString2Bytes(String src)
    {
       byte[] ret = new byte[6];
       byte[] tmp = src.getBytes();
       for(int i=0; i<6; ++i )
       {
        ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]);
          }
       return ret;
    }

     
     ====================================================================

    java 格式化输出十六进制数
       // 以16进制输出文件内容, 每16个数换行一次
       for(int i = 0; i < nLen; i++)
       {
        if(i % 16 == 0)
         System.out.println();
        String strHex = new String();
        strHex = Integer.toHexString(chBuf[i]).toUpperCase();
        if(strHex.length() > 3)
         System.out.print(strHex.substring(6));
        else
         if(strHex.length() < 2)
          System.out.print("0" + strHex);
         else
          System.out.print(strHex);
       
        System.out.print(" ");
       }
     输出结果
    -----------------------------------------------------------------------
    FF D8 FF E0 00 10 4A 46 49 46 00 01 01 00 00 01
    00 01 00 00 FF E1 00 E6 45 78 69 66 00 00 49 49
    2A 00 08 00 00 00 05 00 12 01 03 00 01 00 00 00
    01 00 00 00 31 01 02 00 1C 00 00 00 4A 00 00 00
    32 01 02 00 14 00 00 00 66 00 00 00 13 02 03 00
    01 00 00 00 01 00 00 00 69 87 04 00 01 00 00 00
    7A 00 00 00
     =================================
    当我们把string字符串转成byte[]后,要再转成string 通过String.valueof()是实现不了的,只能new string(byte [])。

  • 相关阅读:
    windows下安装rabbitmq
    selectors
    修改Docker默认镜像和容器的存储位置
    eclipse配置jdk的src.zip源代码步骤
    Zookeeper WINDOWS 安装配置
    zookeeper windows 入门安装和测试
    zookeeper集群搭建(windows环境下)
    ant使用指南详细入门教程
    linux查看系统版本和系统位数
    suse linux 命令
  • 原文地址:https://www.cnblogs.com/mmjx/p/2254309.html
Copyright © 2011-2022 走看看