在Java中用ASCII和Hex形式初始化一个字节有什么区别吗?
我目前正在使用 Java 通过串行通信与设备进行通信。我必须以字节数组发送数据包。我做了以下操作:
byte[] packet = new byte[3];
packet[0] = 'C'; //char form
packet[1] = 'C'; //char form
packet[2] = '2'; //char form
与以这种方式初始化有什么区别:
byte[] packet = new byte[3];
packet[0] = 0x43; //hex form
packet[1] = 0x43; //hex form
packet[2] = 0x32; //hex form
值应该是相同的,对吧?
I am currently using Java to communicate with device through Serial communication. I have to send packet in byte array. I did the following :
byte[] packet = new byte[3];
packet[0] = 'C'; //char form
packet[1] = 'C'; //char form
packet[2] = '2'; //char form
Is there any difference from initialize in this way :
byte[] packet = new byte[3];
packet[0] = 0x43; //hex form
packet[1] = 0x43; //hex form
packet[2] = 0x32; //hex form
The value should be the same, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,它们完全一样。两者都会转换为具有相同值的
int
。Yes, they are absolutely the same. Both get converted to an
int
with the same value.