// 字符串转换成16进制文字列的方法 public String toHex(String str) { String hexString="0123456789ABCDEF"; byte[] bytes=str.getBytes(); StringBuilder hex=new StringBuilder(bytes.length * 2); for(int i=0;i<bytes.length;i++) { hex.append(hexString.charAt((bytes[i] & 0xf0) >> 4)); // 作用同 n / 16 hex.append(hexString.charAt((bytes[i] & 0x0f) >> 0)); // 作用同 n hex.append(' '); //中间用空格隔开 } return hex.toString(); } //将16进制数组转换为字符串 public static String decode(String bytes) { String hexString="0123456789ABCDEF"; 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)))); //将每3位(第3位为空格)中的前2位16进制整数组装成一个字节 for(int i=0;i<bytes.length();i+=3) { baos.write((hexString.indexOf(bytes.charAt(i))<<4 | hexString.indexOf(bytes.charAt(i+1)))); } return new String(baos.toByteArray()); }
//byte转16进制 public void byteToHex(byte inPut[], byte outPut[], int len){ int i = len, j = 0, tmp; for (i = 0; i < len; i++) { tmp = inPut[i]; switch((tmp >> 4) & 0x0f) { case 0: outPut[j++] = '0'; break; case 1: outPut[j++] = '1'; break; case 2: outPut[j++] = '2'; break; case 3: outPut[j++] = '3'; break; case 4: outPut[j++] = '4'; break; case 5: outPut[j++] = '5'; break; case 6: outPut[j++] = '6'; break; case 7: outPut[j++] = '7'; break; case 8: outPut[j++] = '8'; break; case 9: outPut[j++] = '9'; break; case 10: outPut[j++] = 'A'; break; case 11: outPut[j++] = 'B'; break; case 12: outPut[j++] = 'C'; break; case 13: outPut[j++] = 'D'; break; case 14: outPut[j++] = 'E'; break; case 15: outPut[j++] = 'F'; break; default: break; } switch(tmp & 0x0f) { case 0: outPut[j++] = '0'; break; case 1: outPut[j++] = '1'; break; case 2: outPut[j++] = '2'; break; case 3: outPut[j++] = '3'; break; case 4: outPut[j++] = '4'; break; case 5: outPut[j++] = '5'; break; case 6: outPut[j++] = '6'; break; case 7: outPut[j++] = '7'; break; case 8: outPut[j++] = '8'; break; case 9: outPut[j++] = '9'; break; case 10: outPut[j++] = 'A'; break; case 11: outPut[j++] = 'B'; break; case 12: outPut[j++] = 'C'; break; case 13: outPut[j++] = 'D'; break; case 14: outPut[j++] = 'E'; break; case 15: outPut[j++] = 'F'; break; default: break; } } outPut[j] = '\0';
} //byte字符串表示的16进制换成byte数组 public static void hexToByte(byte inPut[],byte outPut[],int len) { int i,j=0; for(i=0;i<len;i++) { //空格的ASC码值为32 if(inPut[i]!=' ') { outPut[j]=(byte) (inPut[i]*16+inPut[++i]); } else { j++; } } } //将16进制数字解码成字符串,适用于所有字符(包括中文) public static String decode(String str,int len) { ByteArrayOutputStream baos=new ByteArrayOutputStream(len/2); //将每2位16进制整数组装成一个字节 for(int i=0;i<len;i+=2) { baos.write((str.charAt(i))<<4 |(str.charAt(i+1))); int a=(str.charAt(i))<<4 |(str.charAt(i+1)); } return new String(baos.toByteArray()); } /** * 将指定字符串src,以每两个字符分割转换为16进制形式 * 如:"2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF, 0xD9} * @param src String * @return byte[] **/ public static byte[] HexString2Bytes(String src) { byte[] ret = new byte[8]; byte[] tmp = src.getBytes(); for(int i=0; i<8; i++) { ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]); } return ret; } /** * 将两个ASCII字符合成一个字节; * 如:"EF"--> 0xEF * @param src0 byte * @param src1 byte * @return byte **/ public 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; }
Android 字符串、byte数组与16进制数组间的转换
最新推荐文章于 2024-08-12 03:28:12 发布