1

I have a byte array,

[102, 100, 51, 52, 48, 48]

Which has the hex string representation:

"fd3400"

Which if I convert it to a number, shows as being 16593920.

However, when I convert it using the snippet below

int iSec = ByteBuffer.wrap(bSec).order(ByteOrder.LITTLE_ENDIAN).getInt();

I get the result: 875783270. The bytes are supposed to be in LSB format, but I can't seem to get the correct value out, as 875783270 != 16593920. I'm getting kind of confused with these data formats.

0

1 Answer 1

3

Byte array contains the byte representation of a string.

In ASCII:

  • 102 == 'f'
  • 100 == 'd'
  • 51 == '3'
  • 52 == '4'
  • 48 == '0'

You should convert from byte array to string and then parse that string using base 16 (hexadecimal).

String hex = new String(arr, "ASCII"); //fd3400
int number = Integer.valueOf(hex, 16).intValue(); //16593920
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your answer, this has indeed gotten me one step closer to the solution! :) I'm now dealing with a new problem though, the hex string appears to be inverted due to the LSB format. Will simply flipping the string before trying to parse it suffice?
The hex string is the same as you mentioned in the question. Here is the working code. ideone.com/g7oMLr. I'm not sure about what are you asking? If you have any other question, please post it in another thread.
Sorry for the confusion, I meant that instead of "fd3400" I'm looking to extract the value of "0034fd".
What is the final number (int) you are looking for?
That would be 13565. Another example: H:7e3900 -> D:14718 and H:0309 -> D:2307
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.