java中 怎么把 ascii码转换为 十六进制
答案:4 悬赏:80 手机版
解决时间 2021-02-19 17:53
- 提问者网友:富士山上尢
- 2021-02-18 23:05
java中 怎么把 ascii码转换为 十六进制
最佳答案
- 五星知识达人网友:酒安江南
- 2021-02-18 23:35
简单的转换,如下就可以:
public static void main(String[] agrs) {
String source = "30";
int code ;
code = Integer.parseInt(source, 16);
// 如果30代表是 16进制的30话,就取16
// 如果30代表是 10进制的30话,就取10
// code = Integer.parseInt(source, 10);
char result = (char) code;
System.out.println(result);
}
根据自己程序健壮性的要求,可以对 int code的范围做出判断,防止 int转向char时产生异常。
public static void main(String[] agrs) {
String source = "30";
int code ;
code = Integer.parseInt(source, 16);
// 如果30代表是 16进制的30话,就取16
// 如果30代表是 10进制的30话,就取10
// code = Integer.parseInt(source, 10);
char result = (char) code;
System.out.println(result);
}
根据自己程序健壮性的要求,可以对 int code的范围做出判断,防止 int转向char时产生异常。
全部回答
- 1楼网友:不甚了了
- 2021-02-19 02:40
我刚好写了
public static String ASCII2HEx(String ASCII2HEX_STRING) {
switch (ASCII2HEX_STRING) {
case "NUL":
return "00";
case "SOH":
return "01";
case "STX":
return "02";
case "ETX":
return "03";
case "EOT":
return "04";
case "ENQ":
return "05";
case "ACK":
return "06";
case "BEL":
return "07";
case "BS":
return "08";
case "HT":
return "09";
case "LF":
return "0A";
case "VT":
return "0B";
case "FF":
return "0C";
case "CR":
return "0D";
case "SO":
return "0E";
case "SI":
return "0F";
case "DLE":
return "10";
case "DC1":
return "11";
case "DC2":
return "12";
case "DC3":
return "13";
case "DC4":
return "14";
case "NAK":
return "15";
case "SYN":
return "16";
case "ETB":
return "17";
case "CAN":
return "18";
case "EM":
return "19";
case "SUB":
return "1A";
case "ESC":
return "1B";
case "FS":
return "1C";
case "GS":
return "1D";
case "RS":
return "1E";
case "US":
return "1F";
case "SP":
return "20";
case "!":
return "21";
case "\\":
return "22";
case "#":
return "23";
case "$":
return "24";
case "%":
return "25";
case "&":
return "26";
case "'":
return "27";
case "(":
return "28";
case ")":
return "29";
case "*":
return "2A";
case "+":
return "2B";
case ",":
return "2C";
case "_":
return "2D";
case ".":
return "2E";
case "/":
return "2F";
case "0":
return "30";
case "1":
return "31";
case "2":
return "32";
case "3":
return "33";
case "4":
return "34";
case "5":
return "35";
case "6":
return "36";
case "7":
return "37";
case "8":
return "38";
case "9":
return "39";
case ":":
return "3A";
case ";":
return "3B";
case "<":
return "3C";
case "=":
return "3D";
case ">":
return "3E";
case "?":
return "3F";
case "@":
return "40";
case "A":
return "41";
case "B":
return "42";
case "C":
return "43";
case "D":
return "44";
case "E":
return "45";
case "F":
return "46";
case "G":
return "47";
case "H":
return "48";
case "I":
return "49";
case "J":
return "4A";
case "K":
return "4B";
case "L":
return "4C";
case "M":
return "4D";
case "N":
return "4E";
case "O":
return "4F";
case "P":
return "50";
case "Q":
return "51";
case "R":
return "52";
case "S":
return "53";
case "T":
return "54";
case "U":
return "55";
case "V":
return "56";
case "W":
return "57";
case "X":
return "58";
case "Y":
return "59";
case "Z":
return "5A";
case "[":
return "5B";
case "\"":
return "5C";
case "]":
return "5D";
case "↑":
return "5E";
case "←":
return "5F";
case "`":
return "60";
case "a":
return "61";
case "b":
return "62";
case "c":
return "63";
case "d":
return "64";
case "e":
return "65";
case "f":
return "66";
case "g":
return "67";
case "h":
return "68";
case "i":
return "69";
case "j":
return "6A";
case "k":
return "6B";
case "l":
return "6C";
case "m":
return "6D";
case "n":
return "6E";
case "o":
return "6F";
case "p":
return "70";
case "q":
return "71";
case "r":
return "72";
case "s":
return "73";
case "t":
return "74";
case "u":
return "75";
case "v":
return "76";
case "w":
return "77";
case "x":
return "78";
case "y":
return "79";
case "z":
return "7A";
case "{":
return "7B";
case "|":
return "7C";
case "}":
return "7D";
case "~":
return "7E";
case "DEL":
return "7F";
default:
return "";
}
}
- 2楼网友:天凉才是好个秋
- 2021-02-19 02:19
public static 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();
}
亲测可用 望采纳
- 3楼网友:一叶十三刺
- 2021-02-19 00:42
是转成十六进制的字符串?
String hex=Integer.toHexString(0x31);
~
~
~
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯