Electronic Design

  
Reprints     Printer-Friendly    Email this Article    RSS        Font Size     What's This?


[Ideas For Design]
Easily Convert Decimal Numbers To Their Binary And BCD Formats

Edmond Vinarub  |   ED Online ID #8912  |   October 18, 2004


Here's a C/C++ program that converts decimal numbers ranging from 0 to 99,999 to binary and BCD formats. Using a simple algorithm in conjunction with pointer arithmetic and bitwise shifting increases the conversion speed without introducing excessive memory overhead and programming complexity.

When decimal numbers are within the range of 0 to 9, their binary and BCD representations are identical, requiring only four bits (0000 to 1001). When the numbers are within the range of 10 to 19, the binary representation varies between four and five bits, such as 1010 to 10011. The BCD representation uses eight bits, such as 0001 0000 to 0001 1001.

If we treat the BCD representation as a straight binary number and compare it to the actual binary representation of the decimal value, we note that there's a decimal difference of 6 between the two numbers. The actual decimal number varies between 10 and 19, while the BCD decimal representation varies between 16 and 25. Increasing the actual decimal range to numbers between 20 and 29 results in a difference of 12 between the binary and BCD representations.

This process continues to 99—such that the difference is equal to the most significant decimal digit multiplied by 6. Thus, 90 decimal would be represented in BCD format by 90 + 9*6 = 144. Decimal numbers between 100 and 109 would be represented in BCD by the actual number + 156 (26*6). Numbers between 110 and 119 convert to BCD values of the actual number + 156 + 6.

If we continue this representational process for decimal range of 0 to 99,999, the algorithm developed for the BCD decimal number is defined by the following equation:

dbcd = (m*9256 + I*516 + b*26 + cc)*6 + x

In this equation, x = the actual decimal value; cc = the integer value of (x modulo 100)/10; b = the integer value of (x modulo 1000)/100; I = the integer value of (x modulo 10000)/1000; and m = the integer value of x/10000.

The program uses two one-dimensional arrays to temporarily store the binary (c[18]) and BCD (bcd[20]) numbers, because they're generated by looping bit-by-bit within their separate conversion functions. The actual bit generation assigned to the appropriate array position is accomplished by right bit shifting, via the array index, the decimal number that's to be converted. This shifted number is then masked or "ANDed" with the hex value of I. Each of these bits is added to the array via their associated pointers. The binary conversion function has an index of 0 to 18, while the BCD conversion function uses an index of 0 to 20. The decimal range of 0 to 99,999 determines these indices.

The program also contains a function called LOCATE, which enables the cursor on the computer display to be positioned at any row or column within the visible screen. The remainder of the program has various print statements that let the binary, BCD, decimal, and BCD-decimal numbers be formatted and aligned in a neat tabular form (see the figure).

For most computers, that actual decimal number range could be easily extended to a value of 232, which is the limit for unsigned long integer values. The equation dbcd would require modification to account for the additional boundary changes as the number increases beyond the value of 99,999. The complete program listing (DecimalConversion.doc) can be found in the online version of this article at www.elecdesign.com.


Reprints   Printer-Friendly  Email this Article  RSS    Font Size   What's This?


  • User Advisory Group To Guide Open Verification Methodology’s Evolution
  • DDR3 and DDR2 Memory IP Bolsters SoC Designs
  • PCB Tools Cross-Probe Between Layout And Schematic
  • Constraint-Driven Flow Targets PCB High-Density Interconnects
  • Cadence Abandons Its Bid To Buy Mentor Graphics
  • Model Extractor For CMOS Sports Improved RF/DC Parameters
  • 45-nm Via-Programmable ASICs Add High-Speed I/O Transceivers To Feature Mix
  • Get Ready For NIWeek
    1) Low-Dropout (LDO) Linear Regulators
    (873 views today)
    2) DNA In Your Gadgets?
    (688 views today)
    3) SDR Transforms Amateur Radio
    (563 views today)
    4) Build A Smart Battery Charger Using A Single-Transistor Circuit
    (260 views today)
    5) Easily Convert Decimal Numbers To Their Binary And BCD Formats
    (198 views today)
    ALL TOP 20



    Reader Comments

    hello sir, Im working on MSP430F169.Im using Phyton software for progaming.Can you tell how to convert hexadecimal to decimal .

    Joshua -August 19, 2008

    please send me the full source of this program

    Shibu -August 12, 2008

    plz send me the java code in hhow to convert decimal to binary. thnx cuz we need it in our midterm xsam equirement, thank you

    Anonymous -August 07, 2008

    please help me...teach me how to create a program that will compute decimal to binary

    Anonymous -August 04, 2008

    hello all, could anyone plz email me the code of converting from decimal numbers to binary, octal and hexadecimal using class stack (which has 2 members functions pop and push). thanks all i appreciate it. My email is tungtran316@gmail.com

    Anonymous -July 25, 2008

    PLZ SEND ME CODING OF "DECIMAL TO BINARY CONVERSION"PROGRAMM.

    GAURAV ANAND -July 25, 2008

    pls send me the code that i asked 5 days ago..its urgent...tnx...

    qlot -July 21, 2008

    hey can you send me the source code for number coonversion? thanks

    Anonymous -July 21, 2008

    write function power2 that takes tow integer arguments number and pow and calculates number*(2^pow) use the shift operator to calculate the result.print the values as integers as bits.

    Anonymous -July 21, 2008

    pls send to me the code for converting the decimal to binary, octal and hexa!!! its urgent need it by friday(july 25)... thanks...

    nicole -July 20, 2008

    NO

    Anonymous -July 13, 2008

    pls send me the code that could convert decimal value to any number system using turbo c language...tnx

    qlot -July 13, 2008

    could u send me the code to convert the bcd into decimal

    Anonymous -July 08, 2008

    can u please send me the c language code for the conversion of binary to decimal & viceversa

    anitha -June 29, 2008

    Hi..can u send me the C language code for the conversion of decimal number to hexadecimal number..

    Anonymous -May 15, 2008

    can you please send me the coding of conversion of decimal to hexadecimal number system on my mail, its urgent

    sandip kapase -May 14, 2008

    Hi again.. I figured it out a different to avoid long divisions. The code here accepts user input in decimal (in string) of any size, ie 123456789, then it converts to packed BCD. The program is written in borland c++ but can be easily converted to c (as long you can get the string decimal into char[]). In this example, I am converting 6 digits... you can change array size for greater than 6 digits.

    //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { unsigned long input = 0; unsigned long res = 0; unsigned char c[6]; BYTE b[6]; BYTE len = 0;

    for(int i=0; i<6; i++) // initialization { b[i] = 0xFF c[i] = '\n'; }

    input = StrToInt(editInput->Text); // get string from user edit box strcpy( c, editInput->Text.c_str()); // convert input string into char[]

    for(int i=0; i<6; i++) // mask out non digits decimal { if((c[i] != '\n') && (c[i]>=0x30 && c[i]<=0x39)) { b[i] = c[i] - 0x30; len++; } else break; }

    for(int i=0; i<len; i++) // conversion to BCD { if(i%2 == 0) { if(i == 0) res = res | b[i]; else res = (res << 4) | (b[i]); if(b[i+1] != 0xFF) res = (res << 4) | (b[i+1]); } } lblOutput->Caption = IntToStr(res); } //---------------------------------------------------------------------------

    bit_flipper -May 06, 2008

    good article though, my number is bigger than 99,999 what is the next higher constant. dbcd = (n*????? + m*9256 + I*516 + b*26 + cc)*6 + x

    I try to figure it out from the explanation but no success. thanks

    bit_flipper -May 02, 2008

    pooja - for PIC uC it better to use add-3 algorithm, use google with keywords "PIC BCD add-3" to find the realisations for PIC.

    Petrucho -April 24, 2008

    I was googling but couldn't find any C realizations of the algorithm, so wrote my own ;) IMPORTANT! Valid input data range: from 0...99'999,

    // By Petrucho ;) 2008 unsigned long ibcd(unsigned long in_data){ unsigned long tmp, rest, acc;

    tmp=in_data/10000; rest=in_data - tmp*10000; acc=tmp*9256; tmp=rest/1000; rest-= tmp*1000; acc+= tmp*516; tmp=rest/100; rest-= tmp*100; acc=(acc + tmp*26 + rest/10)*6 + in_data; //BCD result return acc; }

    Petrucho -April 23, 2008

    i need coding in pic microcontroller to convert decimal value into binary value

    pooja -April 16, 2008

    i need coding in pic microcontroller to convert decimal value into binary value

    pooja -April 16, 2008

    Hello, Please post C# code showing how to add two integer numbers together. I need this for my post-doc.

    GradStudent42 -April 15, 2008

    "Could you send me some C routine to convert BCD to decimal? Thanks..." Sure! ConvertDecimalToBCD{ int myDecimal } { i = DecToBCD(myDecimal); return i; } Oh you're welcome - Glad I could help!!!

    Anonymous -April 15, 2008

    Could you send me some C routine to convert BCD to decimal? Thanks...

    luispacheco -April 11, 2008

    Please, I need the code for converting binary to decimal

    Model -March 27, 2008

    Please send me the answer how to convert the hexadecimal number ABCDEF to binary and octal.

    priya -March 26, 2008

    please tell me a c++ program to convet 32 bit IP address from hexa decimal notation to decimal notation?

    sumit bhutani -March 24, 2008

    can u send me codings of converting hexadecimal to binary number using turbo assembler (pc language)

    Anonymous -March 16, 2008

    Would you please send me the C code for converting from decimal to BCD format (and from BCD format to decimal) if possible.

    Thanks.

    Anonymous -March 10, 2008

    convert binary to decimal

    albert -March 06, 2008

    Iam facing the problem with java programming language codes (net beans) for the following conversions: Decimal numbers to binary,octal,hexadecimal. Binary numbers to decimal,hexadecimal,actal. Hexadecimal numbers to binary,decimal,octal. Octal numbers to binary,hexadecimal,decimal. May any expert help me please please please, i would be needing it a.s.a.p... plsss mail it at jelyn_demn77@yahoo.com

    jelyn_demn77@yahoo.com -March 05, 2008

    Iam facing the problem with java programming language codes (net beans) for the following conversions: Decimal numbers to binary,octal,hexadecimal. Binary numbers to decimal,hexadecimal,actal. Hexadecimal numbers to binary,decimal,octal. Octal numbers to binary,hexadecimal,decimal. May any expert help me please please please, i would be needing it a.s.a.p... plsss mail it at jelyn_demn77@yahoo.com

    jelyn_demn77@yahoo.com -March 05, 2008

    can i ask a code in converting decimal to binary and decimal to octal using assembly language programming?

    Anonymous -March 03, 2008

    how can i convert decimal to binary using the stack class and nodes...please send me the codes..thank you..using the java language..

    ethel -February 19, 2008

    how can i convert decimal to binary using the stack class and nodes...please send me the codes..thank you

    ethel -February 19, 2008

    how do i convert decimal to binary using java and vice versa, plz send me the source code!

    Huiha -February 06, 2008

    how do i convert decimal to binary using java and vice versa, plz send me the source code!

    Huiha -February 06, 2008

    how do i convert decimal to binary using java and vice versa, plz send me the source code!

    Huiha -February 06, 2008

    how can i convert a binary number to a decimal number in programming using assembly language also vice versa

    ola -January 30, 2008

    ayekaye ni e live,u cant giv d right answer to my question

    bad life -January 30, 2008

    help me please...!i need C++ code for converting octal numbers to decimal numbers..please...tia...hoping for your kind...

    jhona -August 09, 2007

    can you please send me a code for converting octal numbers to decimal numbers using C++...please...tia...

    jhona -August 09, 2007

    I need help in c++ programming that how to convert decimal to binary and decimal to octal

    sara -August 02, 2007

    program to covert desimal to binary and decimal to octal please help??

    TOM -August 02, 2007

    Rating Only

    Rating Only -July 30, 2007   (Article Rating: )

    i need to know how to convert 'dec to hex' and 'binary to dec'!

    some kind souls pls help!

    steph -July 29, 2007

    may i ask a code for binary, hex and octal to decimal using visual basic?

    Anonymous -July 26, 2007

    Rating Only

    Rating Only -July 26, 2007   (Article Rating: )

    i need to get a program in c language to get the conversion of binary to decimal, binary to octal and binary to hexadecimal

    Anonymous -July 18, 2007

    Please I need the above conversion codes in Vb6 and Vb.NET ie conversion from binary to decimal and vice versa.

    emma -July 11, 2007

    looosers your programs dont work...^_^

    Anonymous -June 24, 2007

    looosers your programs dont work...^_^

    Anonymous -June 24, 2007

    program that converts dec to binary pls,,& hex to dec!

    glaisa -June 20, 2007   (Article Rating: )

    Rating Only

    Rating Only -June 20, 2007   (Article Rating: )

    decimal to binary pls....

    glai -June 20, 2007

    do you help me about the real numbers? [e.g. 102.23]

    paula -May 24, 2007

    Please send me a diagram of a NAND gate implementation of the BCD ic of 7 segment display or transistorized means either.Thanks

    donald -May 09, 2007

    Iam facing the problemwith java programming language codes for the following conversions: Decimal numbers to binary,octal,hexadecimal. Binary numbers to decimal,hexadecimal,actal. Hexadecimal numbers to binary,decimal,octal. Octal numbers to binary,hexadecimal,decimal. May any expert help me please!

    NASSORO,Juma Sabih -May 02, 2007

    My name is Anzameni Meyasi from Arusha,Tanzania.I'm hereby requesting you to help me writing a program by using java codes converter that convert the following numbers;Binary numbers to their decimal numbers ,binary numbers to their octal numbers,binary numbers to their hexadecimal numbers

    ANZAMENI MEYASI -April 30, 2007

    Please send me the program codes to convet decimal numbers to binary,hexadecimal,octal numbers.

    THABIT MUTA -April 30, 2007

    How to convert hexadecimal number to its BCD format by using Shift and add method?

    Praveen Kumar -April 27, 2007   (Article Rating: )

    i am engineer

    tien -April 13, 2007

    How can i convert a binary into decimal in java?

    Anonymous -March 26, 2007

    Hi can I get the source code to convert decimal to binary,decimal to octal and decimal to hexadecimal using java.

    Nana -March 23, 2007

    Can I get the source code to convert decimal to binary,decimal to ocal and decimal to hexadecimal in java.

    Anonymous -March 23, 2007

    Gw basic program code to convert the decimal numbers into binary

    manni -March 21, 2007

    code for decimal to binary convert in gw basic?

    Suraj -March 21, 2007

    you suck

    Anonymous -March 20, 2007

    In this line, you have an error that fixes 3.

    Line # = 105

    Line: "scanf ("%lu", &x); /'/ Input decimal no. for range conversion"

    you have an apostrophe between the slashes to comment.

    Otherwise, program won't run, and kinda a hassle to go through all numbers to get to yours if it's like over 100

    Anonymous -March 16, 2007

    send me a simple program that converts decimal number to binary number in c,c++

    Anonymous -March 14, 2007

    send me a simple coding that convert decimal into binary in c++ program?

    mm -March 14, 2007   (Article Rating: )

    pls help me. a c program that converts a number to 32-bit representation using 10s complement.....

    Anonymous -March 05, 2007   (Article Rating: )

    how to covert decimal numbers into binary

    Anonymous -February 24, 2007

    Hi, please send more work to India so they can ask everyone else how to do it, thanks!

    Heywood -February 22, 2007   (Article Rating: )

    could you please send me decimal to binary conversion code in c++

    Anonymous -February 07, 2007

    make ur site a better place by using ur mind

    Anonymous -December 12, 2006   (Article Rating: )

    just stupid articles nothing useful

    Anonymous -December 12, 2006   (Article Rating: )

    pls help me convert a number from any base to another using continued division

    yonas -December 06, 2006

    can u please send VB6 codes for following conversions 1. decimal to binary 2. decimal to octal 3. decimal to hexadecimal 4. binary to decimal 5. binary to octal 6 binary to hexadecimal 7. octal to binary 8 octal to decimal 9. octal to hexadecimal 10 hexadecimal to binary 11. hexadecimal to decimal 12 hexadeimal to octal

    sia -November 29, 2006

    your web site does not help me any give up and let it die!

    Anonymous -November 27, 2006

    pls pls pls help me.. i need to convert 8 bit binary to its BCD format.. using 7483 and basic gates.. thanks

    wati200x -November 22, 2006

    plz send the binary number to decimal number& hex number to deciamal conversion in vc++ code

    sheshukumar -November 22, 2006

    hi sir, can you send me a code for converting hexa number into decimal by visual c++ program .plz urgent.thank you.

    Sos -Novenber13 , 2006

    sheshukumar -November 12, 2006   (Article Rating: )

    hi can any one tell me how to convert only decimal to binary and the logic explained please

    Anonymous -November 09, 2006

    Good day for you Sir,

    Sir please send me the a c++ programme that convert decimal numbers to binary numbers. Thanks alot From Inam ullah iscorpion_586@yahoo.com

    Inam ullah -November 01, 2006   (Article Rating: )

    HI please , can you send me a code for converting binary numbers into decimal , octal and hexadecimal by visual c++ program

    Sos -October 31, 2006

    putang ina nyong lahat!!!!!!!!!!!!!!!!!!11

    Anonymous -October 15, 2006

    Can you also send me a code of coverting decimal to binary and vice versa using visual basics?

    Anonymous -October 02, 2006   (Article Rating: )

    Can you send me a code of converting binary numbers to decimal numbers and vise versa using visual basic.

    Messag Kamati -October 01, 2006

    Your all nerds

    Anonymous -September 29, 2006

    hey it is jojo. i just want to say that this website rocks. i will request it to all my friends.

    jojo -September 27, 2006

    i love this website. it is so useful and i found all that i needed. i will request this site to all my midget friends that live in LA. thanks again. 11100100

    boom- boom -September 27, 2006

    i love cupcakes. they are the boom. vanilla and chocolate frosting is the best kind of all time.

    bob -September 27, 2006   (Article Rating: )

    hi i want program c++ that converter algrithm BCD to binary

    Anonymous -September 26, 2006

    hi there.. anyone could help me in my project?/.. i dont know how to canvert ABCD up to Z to binary, decimal, octal, hexadecimal..??? please help me.. just send me at my emaill.. sulkybabysweety@yahoo.com

    thanks..

    babysweety -September 16, 2006

    how convert decimal into hexadecimal in c++ program?

    Anonymous -September 14, 2006

    how convert decimal into hexadecimal in c++ program?

    Anonymous -September 14, 2006

    how can i convert decimal into hexadecimal?:(

    Anonymous -September 14, 2006

    pl

    Anonymous -September 14, 2006

    pls can anyone help me.. i wanna knw how to convert binary number to hex using C programming..

    Anonymous -September 08, 2006

    How can i convert decimal values to BCD

    chat -August 28, 2006

    i need a java code that converts from binary to decimal

    Alex HELP! -August 21, 2006

    i need a c++ code that converts bases between binary,decimal,octal and hexadecimal.please.

    Anonymous -August 13, 2006

    pls send me a simple c++ coding to convert decimal numbers to binarysystem

    raj -August 11, 2006

    how can i convert characters into bcd code and octal equivalent

    Anonymous -August 01, 2006

    can you please send me a C source code on converting decimal numbers to binary using stack?!I am hoping to receive that...thanks

    debz -July 31, 2006

    hey .. can u tel me hw to convert binary to decimal.?

    Anonymous -July 23, 2006

    I need to know how to program a decimal convert to binary,octal,hexadecimal. and sum of 2 binary numbers.. Getting the decimal value of hexadecimal digit,,

    pamee grace tab -May 08, 2006

    hi can any one give me a code for converting a decimal to hexadecimal,binary and octal in a single program

    rupan -April 19, 2006

    can you please send algorithms for following conversions to my mail: decimal to binary and binary to decimal

    maryam -April 15, 2006

    can you please send coding for following conversions to my mail: decimal to binary and binary to decimal

    maryam -April 15, 2006

    Any body intrested in helping me babes.

    FARMAN -March 13, 2006

    plz send the data conversions of decimal to hexadecimal and of hexadecimal to decimal

    apoorva -March 03, 2006

    Convert from decimal to binary using data structure, push and pop the binary number.

    Amid -February 08, 2006

    Convert from decimal to binary using data structure, push and pop the binary number.

    Amid -February 08, 2006   (Article Rating: )

    hi

    Anonymous -January 12, 2006

    i want dcb and dco c programs

    jehan zaib -December 14, 2005   (Article Rating: )

    pls send m the program of DCB, BCD or decimal to hex,oct, etc.

    rajiv -December 12, 2005   (Article Rating: )

    pls send me a simple c coding to convert decimal numbers to binarysystem.

    Anu -November 19, 2005   (Article Rating: )

    pls send the simply c program code to convert the decimal numbers into binary,octal,hexadecimal

    baranee -November 17, 2005

    can u please send coding for following conversions to my mail: 1 decimal to binary 2 decimal to octal 3 decimal to hexadecimal 4 binary to decimal 5 binary to octal 6 binary to hexadecimal 7 octal to binary 8 octal to decimal 9 octal to hexadecimal 10 hexadecimal to binary 11 hexadecimal to decimal 12 hexadeimal to octal please send it.i didnot get the answer with my coding

    Anonymous -October 29, 2005

    pls help me my problem about converting the decimal number to binary, octal, hexadecimal and vice versa. I need your help! Thanks!

    Atan -September 24, 2005

    how can i i make a device using the program i.e. how can i copy it to a chip?

    Anonymous -September 09, 2005

    hi!!!! i used your code. my program is working properly but the conversion takes much time which is not afforadable in my real time systems. Can you please any other faster algorithm for Decimal(999999) to BCD conversion?

    selva

    selva -April 05, 2005

    How can I convert binary to BCD?

    Anonymous -February 14, 2005

    How can I convert a long integer to its corresponding BCD Java?

    Shantanu -November 19, 2004

    That's an interesting way of doing it. I usually do the following:

    main() { unsigned int v; unsigned int value; unsigned int bcd; int i;

    printf("type number at all?\n"); scanf ("%lu", &value);

    for (v = 0; v <= value; v++) { unsigned int t, work; bcd = 0; work = v; for (i = 4; i >= 0; i--) { bcd >>= 4; t = work / 10; bcd |= (work - t * 10) << 28; work = t; } printf("%8d decimal -> %8x hex -> %8x bcd\n", v, v, bcd); } }

    Lawrence D. Lopez -October 23, 2004

    POST YOUR COMMENTS HERE
    Name:

    Email:
    Your Comments:

    Enter the text from the image below


    Please refresh the page if you have trouble reading this text.

    Search Electronic Design
         
      
     
    Web Seminar
    Sponsored By:
    Title: Read Pacing: A Performance Enhancing Feature of PCI Express Gen 2 Switch Devices
    Speakers: 
    Date: 07/01/08
    Register: 

    Electronic Design Europe Electronic Design China EEPN Power Electronics Auto Electronics Microwaves & RF RF Design
    Schematics Find Power Products Military Electronics Featured Vendors EE Events Free Design Resources