当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Wrong Decimal Representation of Hexadecimal Value

问题描述:

I have a small data set in HexaDecimal Representation -

byte[] bb = new byte[] { (byte)0x7D, (byte)0x44, (byte)0xCC };

To understand how the values are coming in Decimal Representation, I did the following -

Log.i("DECIMAL VALUE of 7D>>", buf[i] + "");

Log.i("DECIMAL VALUE of 44>>", buf[i+1] + "");

Log.i("DECIMAL VALUE of CC>>", buf[i+2] + "");

And what it printed was -

DECIMAL VALUE of 7D>> 125

DECIMAL VALUE of 44>> 68

DECIMAL VALUE of CC>> -52

Looking into the site -

http://hextodecimal.com/index.php?hex=CC

The Decimal Representation of byte CC is 204.

In my application I am getting Index Out Of Bounds Exception just because index at -52 doesn't exist in the bounds.

So how is this byte coming negative and what is the clear solution.

网友答案:

Bytes in Java are signed i.e. they range from -128 to 127. If you want to represent the number 204, use an int.

int[] bb = new int[] { 0x7D, 0x44, 0xCC };

If you really want to store the data as bytes, you can convert it to unsigned where you use it to index in an array.

yourArray[bb[1] && 0xFF]

See the following question: How to Convert Int to Unsigned Byte and Back

分享给朋友:
您可能感兴趣的文章:
随机阅读: