byte - Java stripping zeros from function -
i'm trying flip bytes around in java , function have working correctly bytes , failing others.
the function using this:
public static int foldinbyte(int m, int pos, byte b) { int tempint = (b << (pos * 8)); tempint = tempint & (0x000000ff << (pos * 8)); m = m | tempint; return m; }
and code implements is:
byte[] bitmaskarray = new byte[]{ bytebuffer.get(inputindex), bytebuffer.get(inputindex + 1), bytebuffer.get(inputindex + 2), bytebuffer.get(inputindex + 3)}; int tempint = 0; tempint = foldinbyte(0, 3, bitmaskarray[3]); tempint = foldinbyte(tempint, 2, bitmaskarray[2]); tempint = foldinbyte(tempint, 1, bitmaskarray[1]); tempint = foldinbyte(tempint, 0, bitmaskarray[0]); bitmask = tempint;
the bytes being read bytebuffer byteorder being little endian.
for example, bytes 00 01 b6 02 set bitmask to: 2b60100 - works in program.
however, if bytes a0 01 30 00, bitmask set to: 3001a0 - has stipped last 0 bitmask.
is there way can stop java stipping off trailing zeros?
i hope makes sense.
thanks
tony
the zeros not being stripped -- both examples cited correct.
- 00 01 b6 02 4-byte little-endian 2b60100
- a0 01 30 00 4-byte little-endian 3001a0
the zeros there, not being printed. system.out.print family of calls not print leading 0 digits.
i might mention method needlessly complex. here single method computes same value:
static int extractlittleendian4(byte[] buf, int index) { int = buf[index+0]&0xff, b = buf[index+1]&0xff, c = buf[index+2]&0xff, d = buf[index+3]&0xff; return | (b << 8) | (c << 16) | (d << 24); }
Comments
Post a Comment