当前位置: 技术问答>java相关
这段程序怎么造成会出界阿?(short类型转成byte[2])
来源: 互联网 发布时间:2015-05-10
本文导语: short st = -27995; byte[] stBt = new byte[2]; st2Bt = ShortToBytes.shortTo(st2); //此处溢出 请问是为什么?short to byte[2]转换如下 public class ShortToBytes { public static byte[] shortTo(short s) { byte[] buf = new byte[2]; i...
short st = -27995;
byte[] stBt = new byte[2];
st2Bt = ShortToBytes.shortTo(st2);
//此处溢出
请问是为什么?short to byte[2]转换如下
public class ShortToBytes {
public static byte[] shortTo(short s) {
byte[] buf = new byte[2];
int pos;
for (pos = 0 ; pos >= 8;
if (s == 0) break;
}
byte[] rt = new byte[pos + 1];
for (int j = 0 ; j >> 8) & 0xFF);
buf[1] = (byte)(s & 0xFF);
return buf;
}
}
byte[] stBt = new byte[2];
st2Bt = ShortToBytes.shortTo(st2);
//此处溢出
请问是为什么?short to byte[2]转换如下
public class ShortToBytes {
public static byte[] shortTo(short s) {
byte[] buf = new byte[2];
int pos;
for (pos = 0 ; pos >= 8;
if (s == 0) break;
}
byte[] rt = new byte[pos + 1];
for (int j = 0 ; j >> 8) & 0xFF);
buf[1] = (byte)(s & 0xFF);
return buf;
}
}
|
buf长度是2,而rt的长度可能是3?
莫非是有符号移位高位补1?
莫非是有符号移位高位补1?