0

I am trying to convert the following array to long number.

my expected results: 153008 ( I am not sure if it's decimal or hexa)

my actual results (what I am getting): 176

this is what I did, what am I doing wrong?

  byte bytesArray [] = { -80,85,2,0,0,0,0,0};  

  long Num = (bytesArray[7] <<56 |
                bytesArray[6] & 0xFF << 48 |
                bytesArray[5] & 0xFF << 40 |
                bytesArray[4] & 0xFF << 32 |
                bytesArray[3] & 0xFF << 24 |
                bytesArray[2] & 0xFF << 16 |
                bytesArray[1] & 0xFF << 8 |
                bytesArray[0] & 0xFF << 0 );
Ohad
  • 1,563
  • 2
  • 20
  • 44

1 Answers1

1

Add brackets like this:

    long num = (bytesArray[7] << 56 |
                  (bytesArray[6] & 0xFF) << 48 |
                  (bytesArray[5] & 0xFF) << 40 |
                  (bytesArray[4] & 0xFF) << 32 |
                  (bytesArray[3] & 0xFF) << 24 |
                  (bytesArray[2] & 0xFF) << 16 |
                  (bytesArray[1] & 0xFF) << 8 |
                  (bytesArray[0] & 0xFF) << 0 );

Otherwise you do << on 0xFF so it becomes really big, before you do bytesArray[x] & [large number] which always evaluates to 0.

Result 153008, hence success!

Stefan
  • 2,395
  • 4
  • 15
  • 32