日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java hex_使用java实现hex和ascii码的转换

發布時間:2023/12/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java hex_使用java实现hex和ascii码的转换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原標題:使用java實現hex和ascii碼的轉換

幾乎很少寫JAVA代碼,第一是確實不會,第二感覺JAVA寫起來不爽(較python、golang),但總有萬不得已必須要用java的時候。這里記錄下使用java實現的hex十六進制和acsii碼之間的轉換(代碼主要還是從網上找來的,簡單改吧改吧)。

一、ASCII to Hex

這里是將ascii碼轉換為十六進制值,代碼如下:

private static String asciiToHex(String asciiStr) {

char[] chars = asciiStr.toCharArray();

StringBuilder hex = new StringBuilder();

for (char ch : chars) {

hex.append(Integer.toHexString((int) ch));

}

return hex.toString();

}

中間使用十進制進行了轉換一下。

二、hex to ascii

代碼如下:

private static String hexToAscii(String hexStr) {

StringBuilder output = new StringBuilder("");

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

String str = hexStr.substring(i, i + 2);

output.append((char) Integer.parseInt(str, 16));

}

return output.toString();

}

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

三、完整示例

public class StringToHex{

public String convertStringToHex(String str){

char[] chars = str.toCharArray();

StringBuffer hex = new StringBuffer();

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

hex.append(Integer.toHexString((int)chars[i]));

}

return hex.toString();

}

public String convertHexToString(String hex){

StringBuilder sb = new StringBuilder();

StringBuilder temp = new StringBuilder();

//49204c6f7665204a617661 split into two characters 49, 20, 4c...

for( int i=0; i

//grab the hex in pairs

String output = hex.substring(i, (i + 2));

//convert hex to decimal

int decimal = Integer.parseInt(output, 16);

//convert the decimal to character

sb.append((char)decimal);

temp.append(decimal);

}

System.out.println("Decimal : " + temp.toString());

return sb.toString();

}

public static void main(String[] args) {

StringToHex strToHex = new StringToHex();

System.out.println("\n***** Convert ASCII to Hex *****");

String str = "My site is www.361way.com,Fucking Java!";

System.out.println("Original input : " + str);

String hex = strToHex.convertStringToHex(str);

System.out.println("Hex : " + hex);

System.out.println("\n***** Convert Hex to ASCII *****");

System.out.println("Hex : " + hex);

System.out.println("ASCII : " + strToHex.convertHexToString(hex));

}

}

上面的代碼執行后,輸出如下:

[root@localhost tmp]# java StringToHex

***** Convert ASCII to Hex *****

Original input : My site is www.361way.com,Fucking Java!

Hex : 4d792073697465206973207777772e3336317761792e636f6d2c4675636b696e67204a61766121

***** Convert Hex to ASCII *****

Hex : 4d792073697465206973207777772e3336317761792e636f6d2c4675636b696e67204a61766121

Decimal : 77121321151051161013210511532119119119465154491199712146991111094470117991071051101033274971189733

ASCII : My site is www.361way.com,Fucking Java!

看到上面的示例,是不是想到上面的代碼的一個應用場景 ---- 密碼簡單加密。返回搜狐,查看更多

責任編輯:

總結

以上是生活随笔為你收集整理的java hex_使用java实现hex和ascii码的转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。