Android Question Integer to BCD Convertion

sunil thomas

Member
Licensed User
Longtime User
I need to convert an Integer to BCD, It is to display As Binary Coded Decimal to User in label or Button text. Is there any library available?
 

Beja

Expert
Licensed User
Longtime User
@Manfred
decimal to Binary coded decimal. (not decimal to binary)

Binary Coded Decimal is used to display a decimal value on a seven-segment display (see picture)
Decimal values are from 0 to 9
BCD is a seven bit value.. you use a byte with the MSB is "Don't Care"

The following table and image explain the values of BCD and equivalent decimal values.
Then you can easily code a function in b4a to do the conversion.

Note there is a mistake in the table, because when representing the decimal number "1" then B and C, both should be "1".. on the table only "B" is "1"

 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Ahh, ok. I just googled bdc. Did not read all... Just saw the binary maps and thought the TO is searching for the binarystring.
The TO wrote: It is to display As Binary Coded Decimal to User in label or Button text
So he want to show this BCD-Graphic on a label or button?

Thank you for correcting me
 
Last edited:
Upvote 0

Beja

Expert
Licensed User
Longtime User
So he want to show this BCD-Graphic on a label or button?

It doesn't matter.. technically it is called 7-segment display.. then he can design the display in any way he wishes.
The required function is to encode the 10 bits decimal (0 to 9) into 7 bits so they can represent human-readable digit.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Since we only have 10 values, then I would use a simple select case.

Select DecVal

case 0
BinVal = FE
case 1
BinVal = 60
case 2
BinVal = 6D
case 3
BinVal = F9
case 4
BinVal = 33
case 5
BinVal = DB
case 6
BinVal = DF
case 7
BinVal = F0
case 8
BinVal = FF
case 9
BinVal = FD

BinVal, in fact is a hexadecimal representation of the BCD value.
 
Last edited:
Upvote 0

sunil thomas

Member
Licensed User
Longtime User
I am getting Data from Pic Micro controller to Android App via Blue tooth Module, The Data is having 30 Bytes, in that 24 bytes makes 12 ID's each with 16 bits integer(in PIC XC8 C Compiler), representing 1000 to 9999 in Decimal. This binary/Hex value of 14 bits length, should be converted to Human readable form as string to be displayed in Label and also in button texts( 12 buttons are there for displaying, why button - there I use button press to copy this string to dial screen from there I can dial out to do other function to Pic Micro itself.
I need normal string only not 7 segment code, as it is only use full and needed normally in Micro controller coding. That's only a code to drive the segment LED Hardware. Here i don't want to use it.
BCD is 0 to 9 digit only having 4 bits.Hex value 0x3E8 = dec1000, in BCd, it need to have 4 nibbles to hold the value, ie 0x01, 0x0, 0x0, 0x0, or 4 bytes it self. As hex it needed only 14 bits to hold value.
I searched for any library if available, but now I found the way to do this, as
if, short int is 0x03E8,
first divide by dec10, put modulus in the least BCD byte, then
divide the result by dec10, put the modulus in byte 1 of BCD, then
again divide the result by dec10, put the modulus in byte 2 of BCD, then
again divide the result by dec10, put the modulus in byte 3 of BCD that's all the logic.
Now I have to implement code for it.
Thank you All for the response.
 
Upvote 0

sunil thomas

Member
Licensed User
Longtime User
Sorry, there was a mistake in the conversion logic i wrote, as we divide by 10 finally the divident will be undivisible by the divisor 10, then should stop and put the divisor to the current unloaded higher BCD byte. To find when the divident becomes undivisible is still not clear for me, I have to see sample code in C, then will code. If there were any pre-written code it were easy.
Thank you.
 
Upvote 0

sunil thomas

Member
Licensed User
Longtime User
I wrote code. It's working well.

Sub hextobcd( num As Int)
'unsigned Char bcdarray(4);
'While(num > 0)
'{
'value = num % 10;
'num /= 10;
'i = i + 1;
'}
'}
cnt_i=0
For k=0 To 3
bcdarray(k)=0
Next

Do While num > 0
'bcdarray(cnt_i)= num Mod 10
bcdarray(cnt_i)= num Mod 10
num =num/10
cnt_i = cnt_i+1
Loop

End Sub


'calling sub:
Label3.text=""
hextobcd( 0x17AB) ' here calling the module, the result is stored in bcdarray()

For k=3 To 0 Step -1 ' in reverse order to Label Text
Label3.text=Label3.text& bcdarray(k) 'clearing the string in Label3
Next

Label3.text=Label3.text ' for placing a break point

' the result:
'if I give 0x3E8 ie 1000
'gives 1000 displayed in the label
'if I give 0x17AB ie dec6059
'gives 6059 displayed in the label text
 
Upvote 0

sunil thomas

Member
Licensed User
Longtime User
Now I wrote it as a function witch return string but only up to 4 BCD.

'module
Sub int_to_bcd( num As Int) As String
Dim j As Int
j=0
Dim st As String
Dim arr(4) As Char
st=""
Do While num > 0
arr(j)=num Mod 10
num= num/10
j= j+1
Loop
For i=3 To 0 Step -1 ' in reverse order to string Text
st= st& arr(i) 'loading to the string
Next
Return st' return
End Sub

' using the module ' Label3 is previously defined in Sub Process_Globals
Label.text= ""
Label3.Text= int_to_bcd( 0x3E8)
Label3.Text= Label3.text& " "
Label3.Text= Label3.text& int_to_bcd( 0x17AB)
Label3.Text= Label3.text& " "
Label3.Text= Label3.text& int_to_bcd( 0x1587)
Label3.Text= Label3.text& " "
Label3.Text= Label3.text& int_to_bcd( 0x535)
Label3.Text= Label3.text& " "
Label3.Text= Label3.text& int_to_bcd( 0x1987)

'this will display in Label as
1000 6059 5511 1333 6535
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
If you only want to display the decimal on 7-Seg LED like display on a label or button, then use special fonts that simulate the 7-Seg display and send your regular text to it, as njdude suggested..
From what you said I now know you don't want general solution that will also enable you to send the bin values to external LED display. So all what you need is a bin2dec or hex2dec function and then use that special font.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…