0

I have a Bluetooth application which is receiving data in Hexa format in this format "01 0D 18 DA F1 10 03 41 0D 00"

convert this into the decimal and display in my app.

I am using this kind of logic.

int myInt = Integer.parseInt(data1,16);

but i want to convert particluar byte like in the above example. I want to decode only "03 41" into decimal and display . please help me out in this.

1
  • Split and take out the elements that you want, and then convert them? Commented Dec 17, 2013 at 6:59

3 Answers 3

1

What you need to do is split the Hexa string and store it in an array like

String[] array=Hex.split("\\s+"); //using space 

and then parse it like

int myInt = Integer.parseInt(array[1]+" "+array[2],16);
Sign up to request clarification or add additional context in comments.

Comments

1

It's simply too big for an int (which is 4 bytes and signed). The maximum value that a Java Integer can handle is 2147483657, or 2^31-1.
So You should use long and then cast it to int

Use

int myInt = (int)Long.parseLong(data1, 16);

hope this helps..!

Comments

1

What you could do is split the String with data1.split(" ") and then get the piece(s) you need. After that, just run those through the Integer.parseInt(input, 16) method and that should be it I think.

For reference, here's the javadoc on String.split()

Comments

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.