您的位置:首页 > 博客中心 > 编程语言 >

JAVA 十六进制与字符串的转换

时间:2022-03-25 08:17

public static String toHexString(int i)以十六进制的无符号整数形式返回一个整数参数的字符串表示形式。
如果参数为负,那么无符号整数值为参数加上 232;否则等于该参数。将该值转换为十六进制(基数 16)的无前导 0 的 ASCII
数字字符串。如果无符号数的大小值为零,则用一个零字符 ‘0‘ (‘/u0030‘)
表示它;否则,无符号数大小的表示形式中的第一个字符将不是零字符。用以下字符作为十六进制数字:
0123456789abcdef
这些字符的范围是从 ‘/u0030‘ 到 ‘/u0039‘ 和从 ‘/u0061‘ 到 ‘/u0066‘。如果希望得到大写字母,可以在结果上调用
String.toUpperCase() 方法:
Integer.toHexString(n).toUpperCase()
参数:
i - 要转换成字符串的整数。
返回:
用十六进制(基数 16)参数表示的无符号整数值的字符串表示形式。

  1 //转化字符串为十六进制编码  
  2 public static String toHexString(String s) {  
  3    String str = "";  
  4    for (int i = 0; i < s.length(); i++) {  
  5     int ch = (int) s.charAt(i);  
  6     String s4 = Integer.toHexString(ch);  
  7     str = str + s4;  
  8    }  
  9    return str;  
 10 }  
 11 // 转化十六进制编码为字符串  
 12 public static String toStringHex1(String s) {  
 13    byte[] baKeyword = new byte[s.length() / 2];  
 14    for (int i = 0; i < baKeyword.length; i++) {  
 15     try {  
 16      baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(  
 17        i * 2, i * 2 + 2), 16));  
 18     } catch (Exception e) {  
 19      e.printStackTrace();  
 20     }  
 21    }  
 22    try {  
 23     s = new String(baKeyword, "utf-8");// UTF-16le:Not  
 24    } catch (Exception e1) {  
 25     e1.printStackTrace();  
 26    }  
 27    return s;  
 28 }  
 29 // 转化十六进制编码为字符串  
 30 public static String toStringHex2(String s) {  
 31    byte[] baKeyword = new byte[s.length() / 2];  
 32    for (int i = 0; i < baKeyword.length; i++) {  
 33     try {  
 34      baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(  
 35        i * 2, i * 2 + 2), 16));  
 36     } catch (Exception e) {  
 37      e.printStackTrace();  
 38     }  
 39    }  
 40    try {  
 41     s = new String(baKeyword, "utf-8");// UTF-16le:Not  
 42    } catch (Exception e1) {  
 43     e1.printStackTrace();  
 44    }  
 45    return s;  
 46 }  
 47 public static void main(String[] args) {  
 48    System.out.println(encode("中文"));  
 49    System.out.println(decode(encode("中文")));  
 50 }  
 51 /* 
 52 * 16进制数字字符集 
 53 */  
 54 private static String hexString = "0123456789ABCDEF";  
 55 /* 
 56 * 将字符串编码成16进制数字,适用于所有字符(包括中文) 
 57 */  
 58 public static String encode(String str) {  
 59    // 根据默认编码获取字节数组  
 60    byte[] bytes = str.getBytes();  
 61    StringBuilder sb = new StringBuilder(bytes.length * 2);  
 62    // 将字节数组中每个字节拆解成2位16进制整数  
 63    for (int i = 0; i < bytes.length; i++) {  
 64     sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));  
 65     sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));  
 66    }  
 67    return sb.toString();  
 68 }  
 69 /* 
 70 * 将16进制数字解码成字符串,适用于所有字符(包括中文) 
 71 */  
 72 public static String decode(String bytes) {  
 73    ByteArrayOutputStream baos = new ByteArrayOutputStream(  
 74      bytes.length() / 2);  
 75    // 将每2位16进制整数组装成一个字节  
 76    for (int i = 0; i < bytes.length(); i += 2)  
 77     baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString  
 78       .indexOf(bytes.charAt(i + 1))));  
 79    return new String(baos.toByteArray());  
 80 }  
 81 // 第二种方法:  
 82 // 将指定byte数组以16进制的形式打印到控制台  
 83 // 复制代码 代码如下:  
 84 public class Util {  
 85    public Util() {  
 86    }  
 87    /** 
 88    * 将指定byte数组以16进制的形式打印到控制台 
 89    * @param hint String 
 90    * @param b byte[] 
 91    * @return void 
 92    */  
 93    public static void printHexString(String hint, byte[] b) {  
 94     System.out.print(hint);  
 95     for (int i = 0; i < b.length; i++) {  
 96      String hex = Integer.toHexString(b[i] & 0xFF);  
 97      if (hex.length() == 1) {  
 98       hex = ‘0‘ + hex;  
 99      }  
100      System.out.print(hex.toUpperCase() + " ");  
101     }  
102     System.out.println("");  
103    }  
104    /** 
105    * @param b byte[] 
106    * @return String 
107    */  
108    public static String Bytes2HexString(byte[] b) {  
109     String ret = "";  
110     for (int i = 0; i < b.length; i++) {  
111      String hex = Integer.toHexString(b[i] & 0xFF);  
112      if (hex.length() == 1) {  
113       hex = ‘0‘ + hex;  
114      }  
115      ret += hex.toUpperCase();  
116     }  
117     return ret;  
118    }  
119    /** 
120    * 将两个ASCII字符合成一个字节; 如:"EF"--> 0xEF 
121    * @param src0 byte 
122    * @param src1 byte 
123    * @return byte 
124    */  
125    public static byte uniteBytes(byte src0, byte src1) {  
126     byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))  
127       .byteValue();  
128     _b0 = (byte) (_b0 << 4);  
129     byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))  
130       .byteValue();  
131     byte ret = (byte) (_b0 ^ _b1);  
132     return ret;  
133    }  
134    /** 
135    * 将指定字符串src,以每两个字符分割转换为16进制形式 如:"2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF, 0xD9} 
136    * @param src String 
137    * @return byte[] 
138    */  
139    public static byte[] HexString2Bytes(String src) {  
140     byte[] ret = new byte[8];  
141     byte[] tmp = src.getBytes();  
142     for (int i = 0; i < 8; i++) {  
143      ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);  
144     }  
145     return ret;  
146    }  
147 }  

 

本类排行

今日推荐

热门手游