几乎很少写JAVA代码,第一是确实不会,第二感觉JAVA写起来不爽(较python、golang),但总有万不得已必须要用java的时候。这里记录下使用java实现的hex十六进制和acsii码之间的转换(代码主要还是从网上找来的,简单改吧改吧)。

一、ASCII to Hex

这里是将ascii码转换为十六进制值,代码如下:

1private static String asciiToHex(String asciiStr) {
2    char[] chars = asciiStr.toCharArray();
3    StringBuilder hex = new StringBuilder();
4    for (char ch : chars) {
5        hex.append(Integer.toHexString((int) ch));
6    }
7    return hex.toString();
8}

中间使用十进制进行了转换一下。

二、hex to ascii

代码如下:

 1public String convertHexToString(String hex){
 2    StringBuilder sb = new StringBuilder();
 3    StringBuilder temp = new StringBuilder();
 4    //49204c6f7665204a617661 split into two characters 49, 20, 4c...
 5    for( int i=0; i<hex.length()-1; i+=2 ){
 6        //grab the hex in pairs
 7        String output = hex.substring(i, (i + 2));
 8        //convert hex to decimal
 9        int decimal = Integer.parseInt(output, 16);
10        //convert the decimal to character
11        sb.append((char)decimal);
12        temp.append(decimal);
13    }
14    System.out.println("Decimal : " + temp.toString());
15    return sb.toString();
16}

其流程是“Hex<==>Decimal<==>ASCII“。

三、完整示例

StringToHex.java 内容如下:

 1public class StringToHex{
 2  public String convertStringToHex(String str){
 3      char[] chars = str.toCharArray();
 4      StringBuffer hex = new StringBuffer();
 5      for(int i = 0; i < chars.length; i++){
 6        hex.append(Integer.toHexString((int)chars[i]));
 7      }
 8      return hex.toString();
 9  }
10  public String convertHexToString(String hex){
11      StringBuilder sb = new StringBuilder();
12      StringBuilder temp = new StringBuilder();
13      //49204c6f7665204a617661 split into two characters 49, 20, 4c...
14      for( int i=0; i<hex.length()-1; i+=2 ){
15          //grab the hex in pairs
16          String output = hex.substring(i, (i + 2));
17          //convert hex to decimal
18          int decimal = Integer.parseInt(output, 16);
19          //convert the decimal to character
20          sb.append((char)decimal);
21          temp.append(decimal);
22      }
23      System.out.println("Decimal : " + temp.toString());
24      return sb.toString();
25  }
26  public static void main(String[] args) {
27      StringToHex strToHex = new StringToHex();
28      System.out.println("\n***** Convert ASCII to Hex *****");
29      String str = "My site is www.361way.com,Fucking Java!";
30      System.out.println("Original input : " + str);
31      String hex = strToHex.convertStringToHex(str);
32      System.out.println("Hex : " + hex);
33      System.out.println("\n***** Convert Hex to ASCII *****");
34      System.out.println("Hex : " + hex);
35      System.out.println("ASCII : " + strToHex.convertHexToString(hex));
36  }
37}

上面的代码执行后,输出如下:

1[root@localhost tmp]# java StringToHex
2***** Convert ASCII to Hex *****
3Original input : My site is www.361way.com,Fucking Java!
4Hex : 4d792073697465206973207777772e3336317761792e636f6d2c4675636b696e67204a61766121
5***** Convert Hex to ASCII *****
6Hex : 4d792073697465206973207777772e3336317761792e636f6d2c4675636b696e67204a61766121
7Decimal : 77121321151051161013210511532119119119465154491199712146991111094470117991071051101033274971189733
8ASCII : My site is www.361way.com,Fucking Java!

看到上面的示例,是不是想到上面的代码的一个应用场景 —- 密码简单加密。