Sarao
Sarao

Reputation: 379

Converting decimal number into ascii in java

I want to break a decimal number and then convert it to ASCII format so that i can view it in text View.

I am getting Battery percentage from a device as decimal.I cant display it directly by passing decimal number, but need to pass ASCII to the text View.

For Eg: I am getting data in a byte array; consider arr[7] has value 98 which means battery is 98% I want to display the value 98 so i need to pass it as 0x39 and 0x38 respectively How can i break 98 into 0x39 and 0x38 respectively. I tried by doing bitwise ending with 0x0f and 0xf0 and then added 0x30 but it gave me wrong value.

Upvotes: 1

Views: 1195

Answers (2)

Mohammad Arman
Mohammad Arman

Reputation: 7065

Try this:

String hexValue = Integer.toHexString(Integer.parseInt("98"));

Then you can spilt it by " "

String[] splited = hexValue.split(" ");

Now you can get splited[0] = 0x39 and splited[1] = 0x38

Upvotes: 0

Benjamin Cheng
Benjamin Cheng

Reputation: 78

Use String.valueOf(byte) to get the string representation of the byte.

If you must have ASCII representation, you can use the toCharArray() method in String.

Upvotes: 3

Related Questions