For example, I would like to convert the int value 12
into a String output of BCD: 00 12
(0x00 0x12).
If I have int value of 256
, it will be 02 56
(which is 0x02 0x56),
or if I have a int value of 999
, it will be 09 99
(0x09 0x99),
9999
would be 99 99
(0x99 0x99).
Right now, my only solution is to create a String array of size 4, and calculate how many characters are there by converting the int value into String. If there are 2 characters, I will add 2 x 0 into the array first before adding the 2 characters, and then make them back into a single String variable.
Basically,
int value = 12;
String output = Integer.toString(value);
// then count the number of characters in the String.
// 4 minus (whatever number of characters in the String, add zeros
// add the characters:
stringArray[0] = "0";
stringArray[1] = "0";
stringArray[2] = "1";
stringArray[3] = "2";
// then, concatenate them back
If there are 3 characters, I will add one 0
into the array first before adding 3 characters. I was wondering if there is any other way?