dylanwolf(),-128是0x80这是16进制的表示,我要的是二进制德表示!! 谁能给我些代码,让我完全将11111111+11111111合并成1111111111111111? 即byte a = b = 11111111(二进制表示)转换为short c = 1111111111111111(二进制) 而且还可以将它转换回来?
给你两个转换的源码 class ShortToBytes { public static byte[] shortToBytesWay(short s) { byte[] buf = new byte[2]; buf[0] = (byte)((s >>> 8) & 0xFF); buf[1] = (byte)(s & 0xFF); return buf; } } class BytesToShort { public static short bytesToShortWay(byte[] source, int startPos, int byteLength) { if(startPos<0 || ( byteLength!=2 ) || source == null || source.length < startPos+byteLength-1) return 0; else return bytesToshort(source[startPos+0], source[startPos+1], byteLength); } public static short bytesToshort(byte b0, byte b1, int byteLength) { if(byteLength != 2) return 0; int i = 0; short out = 0; if(b0<0) i = 256+b0; else i = b0; out += i; if(b1<0) i = 256+b1; else i = b1; out += (i<<8); return out; } }
有没有谁能告诉我怎么将一个byte类型的数值输出显示为二进制形式,即将所有的 111111111之类的输出来?
谁能给我些代码,让我完全将11111111+11111111合并成1111111111111111?
即byte a = b = 11111111(二进制表示)转换为short c = 1111111111111111(二进制)
而且还可以将它转换回来?
class ShortToBytes {
public static byte[] shortToBytesWay(short s) {
byte[] buf = new byte[2];
buf[0] = (byte)((s >>> 8) & 0xFF);
buf[1] = (byte)(s & 0xFF);
return buf;
}
}
class BytesToShort {
public static short bytesToShortWay(byte[] source, int startPos, int byteLength) {
if(startPos<0 || ( byteLength!=2 ) || source == null
|| source.length < startPos+byteLength-1)
return 0;
else
return bytesToshort(source[startPos+0], source[startPos+1], byteLength);
}
public static short bytesToshort(byte b0, byte b1, int byteLength) {
if(byteLength != 2)
return 0;
int i = 0;
short out = 0;
if(b0<0) i = 256+b0; else i = b0;
out += i;
if(b1<0) i = 256+b1; else i = b1;
out += (i<<8);
return out;
}
}