Wednesday, 28 August 2013

Converting Signed byte array content to decimal with shifting in java

Converting Signed byte array content to decimal with shifting in java

I had a byte array with hex decimal values and I converted that array to
decimal values, using the following code
byte[] content = message.getFieldValue( "_Decoder Message" ).data();
int[] decContent = new int[ content.length ];
int i = 0;
for ( byte b : content )
decContent[ i++ ] = b & 0xff;
now the array looks like decContent = [01 00 05 00 00 00] ;
The first 2 indexes of this array is converted and saved as int value as
below
int traceIdlow = decContent[ 0 ] & 0xFF;
int traceIdhigh = decContent[ 1 ] & 0xFF;
int TraceId = traceIdlow | ( traceIdhigh << 8 );
The last 4 indexes needs to be converted to int as well but I am confused
about the shifting whether it should be 8 or 16 .
How can I convert the last 4 indexes to int value so that according to the
above example the value becomes
00 00 00 05 = 5 in decimal.
Any ideas?
Thanks

No comments:

Post a Comment