Integer To BCD Convertion | B4X Programming Forum

B4X Programming Forum
  • Home
  • Products B4A B4i B4J B4R
  • Showcase
  • Store
  • Learn General Guides Video Tutorials Glossary
  • Teach
  • Blog
  • Forums #B4X Discord (unofficial) B4X Facebook Group B4X Github
  • New posts What's new New profile posts Latest activity
  • Members Current visitors New profile posts Search profile posts
Log in Register What's new
  • #B4X Discord (unofficial)
  • B4X Facebook Group
  • B4X Github
Menu Log in Register Install the app Install
  • Welcome to B4X forum! B4X is a set of simple and powerful cross platform RAD tools:
    • B4A (free) - Android development
    • B4J (free) - Desktop and Server development
    • B4i - iOS development
    • B4R (free) - Arduino, ESP8266 and ESP32 development
    All developers, with any skill level, are welcome to join the B4X community.
  • Home
  • Forums
  • B4A - Android
  • Android Questions
You are using an out of date browser. It may not display this or other websites correctly.You should upgrade or use an alternative browser. Android QuestionInteger to BCD Convertion
  • Thread starter sunil thomas
  • Start date Mar 29, 2014
  • Similar Threads Similar Threads
sunil thomas

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? Sort by date Sort by votes DonManfred

DonManfred

Expert
Licensed User Longtime User B4X: Dim i As Int = 664 Log(Bit.ToBinaryString(i)) Regards Manfred If you find my answer useful please click on like. You can even Donate with PayPal :) Create PDF | Create dynamic JPG | My Libraries (Overview) Upvote 0 Beja

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" 7truth.jpg 7truth.gif Last edited: Mar 29, 2014 On Anger Resume Next Upvote 0 DonManfred

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: Mar 29, 2014 Regards Manfred If you find my answer useful please click on like. You can even Donate with PayPal :) Create PDF | Create dynamic JPG | My Libraries (Overview) Upvote 0 Beja

Beja

Expert
Licensed User Longtime User
So he want to show this BCD-Graphic on a label or button? Click to expand...
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. On Anger Resume Next Upvote 0 Beja

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: Mar 29, 2014 On Anger Resume Next Upvote 0 sunil thomas

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

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

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

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

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. On Anger Resume Next Upvote 0 You must log in or register to reply here.

Similar Threads

Erel
  • Article
Android Tutorial Convert collections to json and vice versa
  • Erel
  • Jul 18, 2021
  • Tutorials & Examples
Replies 5 Views 8K Jul 25, 2021 Erel Erel Erel
  • Article
Android Code Snippet [B4X] Convert milliseconds to string
  • Erel
  • Feb 22, 2018
  • Code Snippets
Replies 9 Views 12K Apr 3, 2025 LucaMs LucaMs Erel
  • Article
B4J Library [B4X] BalConverter - Convert the layouts files to JSON (and vice versa)
  • Erel
  • Jun 1, 2014
  • B4J Libraries & Classes
2 3 Replies 55 Views 40K Nov 8, 2024 aeric aeric JonPM
  • Article
Android Tutorial Convert Integer to DIP
  • JonPM
  • Jun 19, 2012
  • Tutorials & Examples
2 Replies 22 Views 23K Apr 7, 2018 klaus klaus J
  • Question
Convert byte code - optimized dex. Error - Need Help Pls :(
  • Joel Fonseca
  • Jul 25, 2011
  • Android Questions
2 Replies 28 Views 34K May 9, 2017 billyn_hk B Share: Facebook Twitter Reddit Pinterest Tumblr WhatsApp Email Share Link
  • Home
  • Forums
  • B4A - Android
  • Android Questions
  • This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register. By continuing to use this site, you are consenting to our use of cookies. Accept Learn more…
Top

Tag » Arduino Convert Decimal To Bcd