# Convert binary number to Decimal number C code using PIC micrcontroller

Status
Not open for further replies.

#### erfan.khalilian69

##### Member level 4
Hi everyone can anyone tell me how to convert binary number to decimal value,
i just need sample code
thanks

#### grieblm

##### Member level 4
Here's the basic algorithm:

Code:
   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

repeat
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:

Points: 2

### sorin0306

Points: 2

#### erfan.khalilian69

##### Member level 4
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 :
now I think I need to convert the dip_read value to decimal
right ?
do you have any idea ?

#### Sink0

##### Full Member level 6
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....

erfan.khalilian69

### erfan.khalilian69

Points: 2

#### engshahrul

##### Banned
If you want convert dip switch, can do like this
i=0;
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;

erfan.khalilian69

### erfan.khalilian69

Points: 2

#### KerimF

Hi everyone can anyone tell me how to convert binary number to decimal value,
i just need sample code
thanks

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?

Edited:
"grieblm" gave a good answer if you replace:

x = binary number with x = dip_read
and
digits = 5 with digits = 3

Last edited:

#### erfan.khalilian69

##### Member level 4
If you want convert dip switch, can do like this

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

#### KerimF

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

I think it is equivalent to

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:

#### engshahrul

##### Banned
HI what is IN1-8 ? is this my array or pins of PIC ?
IN1 is PIC pin.
If IN1-IN8 is same port, ex all PORTB. Just write i=PORTB. That will give value 0-255. (8bit)

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

Status
Not open for further replies.