Android 怎样接收十六进制数据
答案:1 悬赏:50 手机版
解决时间 2021-02-18 14:03
- 提问者网友:暮烟疏雨之际
- 2021-02-17 22:20
Android 怎样接收十六进制数据
最佳答案
- 五星知识达人网友:话散在刀尖上
- 2021-02-17 23:17
发送消息之前转码为16进制
Java code
public static final String encodeHex(String msg) {
byte[] bytes = null;
try {
bytes = msg.getBytes("GBK");
} catch (java.io.UnsupportedEncodingException e) {
e.printStackTrace();
}
StringBuffer buff = new StringBuffer(bytes.length * 4);
String b;
char a;
int n = 0;
int m = 0;
for (int i = 0; i < bytes.length; i++) {
b = Integer.toHexString(bytes[i]);
if (bytes[i] > 0) {
buff.append("00");
buff.append(b);
n = n + 1;
} else {
a = msg.charAt((i - n) / 2 + n);
m = a;
b = Integer.toHexString(m);
buff.append(b.substring(0, 4));
i = i + 1;
}
}
return buff.toString();
}
Java code
public static final String encodeHex(String msg) {
byte[] bytes = null;
try {
bytes = msg.getBytes("GBK");
} catch (java.io.UnsupportedEncodingException e) {
e.printStackTrace();
}
StringBuffer buff = new StringBuffer(bytes.length * 4);
String b;
char a;
int n = 0;
int m = 0;
for (int i = 0; i < bytes.length; i++) {
b = Integer.toHexString(bytes[i]);
if (bytes[i] > 0) {
buff.append("00");
buff.append(b);
n = n + 1;
} else {
a = msg.charAt((i - n) / 2 + n);
m = a;
b = Integer.toHexString(m);
buff.append(b.substring(0, 4));
i = i + 1;
}
}
return buff.toString();
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯