当前位置: 技术问答>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?
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。