蜗牛的壳 - Java中byte使用注意事项 的评论 https://www.snailium.net/other-tech/notes-on-java-byte-type.html zh-CN 目前在做的SYSC 3303 Project是关于使用Java编写TFTP服务器/客户端。其中Java的发送数据包要用byte数组。凭着C语言的扎实基础,想当然的认为byte就是8位无符号整数。... Wed, 31 Mar 2010 02:03:37 -0400 Wed, 31 Mar 2010 02:03:37 -0400 snailium https://www.snailium.net/other-tech/notes-on-java-byte-type.html#comment-90 https://www.snailium.net/other-tech/notes-on-java-byte-type.html#comment-90 Wed, 31 Mar 2010 02:03:37 -0400 snailium [quote=sephiroth]你好,蜗牛大侠,你是07年就遇到了啊,我最近也遇到了,试了下:
byte[] start = new byte[]{-1, -1, -1, -1};
for(byte b:start){
System.out.print(b + ",");
}
System.out.println();
String s = new String(start);
byte[] end = s.getBytes();
for(byte b:end){
System.out.print(b + ",");
}
System.out.println();
然后结果是:
-1,-1,-1,-1,
63,63,63,63,
所以负数字节在做字节数组字符串转换时,java没处理好啊,或者是我理解错误?
这个你还记得当初是怎么解决的吗,我也是要在socket通信里开发一个接口遇到这些问题了,好头疼...多谢.[/quote]
问题出在把“-1”转换成字符串的步骤上
Java不知道“-1”对应的是哪个字符,所以用“?”代替
而“?”的ASCII编码就是63

]]>
https://www.snailium.net/other-tech/notes-on-java-byte-type.html#comment-90#comments
sephiroth https://www.snailium.net/other-tech/notes-on-java-byte-type.html#comment-89 https://www.snailium.net/other-tech/notes-on-java-byte-type.html#comment-89 Mon, 29 Mar 2010 05:34:09 -0400 sephiroth 你好,蜗牛大侠,你是07年就遇到了啊,我最近也遇到了,试了下:
byte[] start = new byte[]{-1, -1, -1, -1};
for(byte b:start){
System.out.print(b + ",");
}
System.out.println();
String s = new String(start);
byte[] end = s.getBytes();
for(byte b:end){
System.out.print(b + ",");
}
System.out.println();
然后结果是:
-1,-1,-1,-1,
63,63,63,63,
所以负数字节在做字节数组字符串转换时,java没处理好啊,或者是我理解错误?
这个你还记得当初是怎么解决的吗,我也是要在socket通信里开发一个接口遇到这些问题了,好头疼...多谢.

]]>
https://www.snailium.net/other-tech/notes-on-java-byte-type.html#comment-89#comments