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 );
0

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

no prob (if this answer was ok, feel free to mark it as Accepted:) )

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.