【发布时间】:2011-08-05 23:51:33
【问题描述】:
我有一个必须与字节数组连接的字符串,所以我尝试了这个
String msg = "msg to show";
byte[] msgByte = new byte[msg.length()];
try {
msgByte = msg.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] command = {2,5,1,5}
byte[] c = new byte[msgByte.length + command.length];
System.arraycopy(command, 0, c, 0, command.length);
System.arraycopy(msjByte, 0, c, command.length, msjByte.length);
for(Byte bt:c)
System.out.println(bt+"");
这是输出:
2 5 1 5 109 115 103 32 ...
但我正在寻找的结果是这个
2 5 1 5 m s g ...
我需要它在一个数组中,因为它被用作蓝牙打印机的命令。
有没有办法,有什么建议吗?
提前致谢! :)
【问题讨论】:
-
你得到了正确的结果。 ascii(109) = 米。这是一个相当低级的接口,将命令代码与字符串混合在一起吗?
-
我的错误,我认为我在数组中缺少 ASCII 代码,因为打印机的示例命令如下所示:byte[] ESC_Z2 = {0x1b, 0x5a, 0x00, 0x51 , 0x05, 0x14, 0x00 ,'m','e','s','s','a','g','e' };所以我尝试发送纯 ASCII 码,它没有工作,但它是由于命令上的参数