【发布时间】:2014-06-22 19:25:01
【问题描述】:
我从蓝牙套接字读取十六进制 (Example: 0x61 0x62 0x63)。
我想得到它对应的 ASCII (Example: a b c)。
如何进行转换?
我试过了:
String s = "0x56 0x49 0x4e 0x31 0x32 0x33 0x46 0x4f 0x52
0x44 0x54 0x52 0x55 0x43 0x4b 0x00 0x38";
StringBuilder sb = new StringBuilder(s.length() / 2);
for (int i = 0; i < s.length(); i+=2) {
String hex = "" + s.charAt(i) + s.charAt(i+1);
int ival = Integer.parseInt(hex, 16);
sb.append((char) ival);
}
String string = sb.toString();
【问题讨论】:
-
查看此 SO 帖子:stackoverflow.com/questions/4785654/…
-
不,这不会从十六进制中删除 0x,因此不会给出正确的结果。
-
投反对票的人应该阅读 cmets 给出的答案,并且可以解释投反对票的原因,以便我了解问题所在。
标签: android sockets hex ascii android-bluetooth