Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Convert binary number to Decimal number C code using PIC micrcontroller

Not open for further replies.


Member level 4
Feb 7, 2011
Reaction score
Trophy points
Activity points
Hi everyone can anyone tell me how to convert binary number to decimal value,
i just need sample code

Here's the basic algorithm:

   x = binary number     // assume the number is unsigned
   d[1..5] = 0               // will contain 5 bcd digits of decimal number
   digits = 5                 // a 16-bit binary number can only produce 5 digits

      d[digits] = x % 10   // extract the lowest digit
      x = x / 10               // shift down the next digit
      digits = digits - 1
   until x = 0

I can also dig out another algorithm that doesn't require the division and modulo operations if needed.
Last edited:
Hi actually this algorithm I can't got the logic , can you give me the sampe ?
I am using dip switch and i want to display it in LCD and count it down until zero.
I just write the code as :
dip_read = PORTB ;
now I think I need to convert the dip_read value to decimal
right ?
do you have any idea ?

No, the conversion does no exist, it is just the way you threat the variable he you are programming. You dont nee any conversion.. let say you have 00000011 on your port, your varible is going to be 0b00000011 and at the same time 0x03 and 3 (decimal). The microcontoller just know binary, all the rest is handled by the compiler to create that abstraction layer to the programmer. The conversion you need is just to convert to an ASCII to print on the display....
If you want convert dip switch, can do like this
if(IN1==1) i=i+1;
if(IN2==1) i=i+2;
if(IN3==1) i=i+4;
if(IN4==1) i=i+8;
if(IN5==1) i=i+16;
if(IN6==1) i=i+32;
if(IN7==1) i=i+64;
if(IN8==1) i=i+128;
return i;
Hi everyone can anyone tell me how to convert binary number to decimal value,
i just need sample code

Do you mean you need the code written in assembly?
And it seems that your maximum number (one byte) could be 255 or less, so it needs 3 decimal digits only... right?

"grieblm" gave a good answer if you replace:

x = binary number with x = dip_read
digits = 5 with digits = 3
Last edited:

HI what is IN1-8 ? is this my array or pins of PIC ?

I think it is equivalent to
dip_read = PORTB ;

I am curious to know what could be the missing point to you in this conversion.
I wished to give you the code, but I write my programs in assembly only and for Atmel C51 MCUs.

You start with just one byte (that is a number in 8-bit binary, from 0 to 255)... right?
This byte will be converted to 3 bytes (or 1 byte + a nibble or half byte) to hold 3 decimal digits. A decimal digit needs 4 bits.
Last edited:

Dim An_Integer as Integer ' First you declare the variable for your integer

An_Integer = Integer(PORTB) ' This takes the values of all 8 pins on portb and converts them into an integer

Think and improve further

Not open for further replies.

Part and Inventory Search

Welcome to