字节数组和十六进制字符串之间的转换

//字节码转换成16进制字符串

  private static String byte2hex(byte[] b) {

    String hs="";

    String stmp="";

    for (int n=0;n<b.length;n++){

      stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));

      if (stmp.length()==1)

        hs=hs+"0"+stmp;

      else hs=hs+stmp;

        //if (n<b.length-1)  hs=hs+":";

      }

    return hs.toUpperCase();

  }

  

  //16进制字符串转换成字节码

  private static byte[] hex2byte(String h) {

    byte[] ret = new byte[h.length()/2];

    for(int i=0; i<ret.length; i++){

    	ret[i] = Integer.decode("#"+h.substring(2*i, 2*i+2)).byteValue();

    }

    return ret;

  }

你可能感兴趣的:(String,byte,hex)