0

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.

sjain
  • 23,126
  • 28
  • 107
  • 185
  • 1
    honestly, you have so much awards and reputation and don't know about java primitives- is this a fake question?... let me see... your profil says "Certifications - Sun Certified Java Programmer (SCJP)"??? – Martin Frank Aug 19 '14 at 10:31
  • @MartinFrank - Not really. But this byte stuff was new specially when building applications in android since a long time that some java stuff gone old for now. But we always learn from things like these. And those who know it they say its small but before that they know how this clinched them wondering oh HOW THAT SO! – sjain Aug 19 '14 at 10:36
  • but how comes that? you have posted question about passing byte via blue tooth some weeks ago... and already forgot? - it was in May 7, just three Month ago... (http://stackoverflow.com/questions/23519202/converting-string-to-char-when-string-is-a-valid-char) ... but ok, no flame war, i guess you are right... it is a question for everybody and had to be asked... – Martin Frank Aug 19 '14 at 11:38
  • @MartinFrank - Alright! However, that was a different question in which I was getting problems in converting String value of Hex Representation into chars so that I can send the commands over bluetooth to be readable by the remote device. Since I am getting such kind of numerous problems of different types while doing bluetooth socket programming, the current question is nothing new although both problems happened while doing same kinda stuff. It's amazing that you noticed that question. OVERWHELMED! :) – sjain Aug 19 '14 at 11:52
  • franky it was really stupid from SUN/Microsystem to create a signed byte [-128..127] that was really not one of their best moments... i must confess, i use byte very often and i'm always annoyed that i have to make extra work to surpress the signing bit... – Martin Frank Aug 19 '14 at 12:02
  • [James-Goslings-Explanation-Of-Why-Javas-Byte-Is-Signed](http://stackoverflow.com/questions/3108297/james-goslings-explanation-of-why-javas-byte-is-signed) :P :P :) – sjain Aug 19 '14 at 12:45

1 Answers1

1

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

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826