我试过了,这样确实可以
我还想问一下:我下面的程序怎么会造成出界?
byte[] st2Bt = 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 < 2 ; pos++) {
buf[pos] = (byte) (s & 0xff);
s >>= 8;
if (s == 0) break;
}
byte[] rt = new byte[pos + 1];
for (int j = 0 ; j <= pos ; j++) {
rt[j] = buf[j];
}
return rt;
}
}
我还想问一下:我下面的程序怎么会造成出界?
byte[] st2Bt = 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 < 2 ; pos++) {
buf[pos] = (byte) (s & 0xff);
s >>= 8;
if (s == 0) break;
}
byte[] rt = new byte[pos + 1];
for (int j = 0 ; j <= pos ; j++) {
rt[j] = buf[j];
}
return rt;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货