Q
Qu0ll
I am stuffing a byte[] with byte, short and int values and then
reconstituting them elsewhere. Everything works fine but I am concerned
that I am not doing it as efficiently as possible.
The code to stuff a short named x into the byte[] buffer (high byte first)
looks like this:
buffer[index++] = (byte)(((short)x & 0xFF00) >> 8);
buffer[index++] = (byte)( (short)x & 0x00FF);
The code to reconstitute the short looks like this:
byte a = buffer[index++];
byte b = buffer[index++];
short as = a;
short bs = b < 0 ? (short)((b & 0x7F) + 128) : b;
x = (short)((as << 8) | bs);
Is this the most efficient way of doing this? I find it annoying that bytes
are signed. I am also concerned that it may not work for all short values.
--
And loving it,
-Q
_________________________________________________
(e-mail address removed)
(Replace the "SixFour" with numbers to email me)
reconstituting them elsewhere. Everything works fine but I am concerned
that I am not doing it as efficiently as possible.
The code to stuff a short named x into the byte[] buffer (high byte first)
looks like this:
buffer[index++] = (byte)(((short)x & 0xFF00) >> 8);
buffer[index++] = (byte)( (short)x & 0x00FF);
The code to reconstitute the short looks like this:
byte a = buffer[index++];
byte b = buffer[index++];
short as = a;
short bs = b < 0 ? (short)((b & 0x7F) + 128) : b;
x = (short)((as << 8) | bs);
Is this the most efficient way of doing this? I find it annoying that bytes
are signed. I am also concerned that it may not work for all short values.
--
And loving it,
-Q
_________________________________________________
(e-mail address removed)
(Replace the "SixFour" with numbers to email me)