# how to convert binary to decimal using c programming

Page 1 of 1

## 6 Replies - 19997 Views - Last Post: 16 October 2010 - 11:05 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=195111&amp;s=b4910bd2b38bd046b9cf0fa528fa8585&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 teoh

Reputation: 0
• Posts: 12
• Joined: 08-October 10

# how to convert binary to decimal using c programming

Posted 15 October 2010 - 05:58 AM

can u explain how to convert binary to decimal using c programming,or give me some material to read thank....
Is This A Good Question/Topic? 0

## Replies To: how to convert binary to decimal using c programming

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: how to convert binary to decimal using c programming

Posted 15 October 2010 - 06:07 AM

Can't bother to search huh? Converting Binary to Decimal

### #3 Mike Chambers

Reputation: 22
• Posts: 59
• Joined: 30-March 09

## Re: how to convert binary to decimal using c programming

Posted 15 October 2010 - 06:29 PM

if you mean from a binary string of ASCII ones and zeroes, this should work:

```#include <stdio.h>

int main() {
char *binstring = "10000001"; //binary for 129
unsigned decimalval = 0;
int stringpos;

for (stringpos=strlen(binstring)-1; stringpos>=0; stringpos--) {
decimalval = decimalval<<1;
if (binstring[stringpos]=='1') decimalval += 1;
}

printf("The binary string %s is equal to %u in decimal.\n", binstring, decimalval);
return 0;
}

```

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12305
• Posts: 45,401
• Joined: 27-December 08

## Re: how to convert binary to decimal using c programming

Posted 15 October 2010 - 06:37 PM

You realize you gave someone homework to copy/paste? I appreciate your willingness to help, but please respect rule #1 of the forum. We are here to help, not provide finished code. If you would like to submit finished code, the snippets section is more appropriate.

### #5 Mike Chambers

Reputation: 22
• Posts: 59
• Joined: 30-March 09

## Re: how to convert binary to decimal using c programming

Posted 15 October 2010 - 07:59 PM

macosxnerd101, on 15 October 2010 - 05:37 PM, said:

You realize you gave someone homework to copy/paste? I appreciate your willingness to help, but please respect rule #1 of the forum. We are here to help, not provide finished code. If you would like to submit finished code, the snippets section is more appropriate.

damn. you're right. yeah i'll be more careful about that. you didn't need to tag my post as unhelpful though (if it was you) because technically speaking it was very helpful to him. <grin> just in a counterproductive way.

This post has been edited by Mike Chambers: 15 October 2010 - 08:01 PM

### #6 Mike Chambers

Reputation: 22
• Posts: 59
• Joined: 30-March 09

## Re: how to convert binary to decimal using c programming

Posted 15 October 2010 - 08:26 PM

teoh, on 15 October 2010 - 04:58 AM, said:

can u explain how to convert binary to decimal using c programming,or give me some material to read thank....

as long as i did your homework for you i guess i should explain how you would have gone about doing it and why that code works.

since in PC compatible computers the least significant bit of an integral value is the rightmost bit, that means the value of it is literally 1. for each bit to the left that you go from that one, it's value doubles. let's take that 8-bit number in the code as an example here:

```1      0      0      0      0      0      0      1    <-least significant bit
^      ^      ^      ^      ^      ^      ^      ^
|      |      |      |      |      |      |      |_____ bit position is worth 1
|      |      |      |      |      |      |
|      |      |      |      |      |      | ___________ bit position is worth 2
|      |      |      |      |      |
|      |      |      |      |      |___________________ bit position is worth 4
|      |      |      |      |
|      |      |      |      |__________________________ bit position is worth 8
|      |      |      |
|      |      |      |_________________________________ bit position is worth 16
|      |      |
|      |      |________________________________________ bit position is worth 32
|      |
|      |_______________________________________________ bit position is worth 64
|
|______________________________________________________ bit position is worth 128
```

now you can see why i made that for loop that started at the first character of that string. from there we can traverse right through the bits no matter how long the string is as it's total value accumulates in our decimalval variable. (at least, up to 16-bits since decimalval was defined as an "unsigned" ... if you change that to "unsigned long" you can handle up to 32-bits with the same code)

as our loop checks to see if each character is a 1 or a 0, we add that literal numerical value to decimalval. if there is another bit after it in the binary string we shift the existing contents of decimalval BITWISE to the left one single bit, which is the same exact thing as multiplying by 2. (which you can see why by looking at that illustration)

the "<<n" operator means to shift the variable's bits to the left n number of bits. you can also use ">>n" if you wanted to shift the contents' bits to the right (equivalent to dividing the value by 2, n number of times while losing any fractional remainder to great proverbial void in the sky) but that wouldn't help us in this case, but now you know that it's there if you need it.

NOTE: yes i know, i noticed i did the code snippet backwards originally, i'm editing it now. the for loop should start at the leftmost bit, not rightmost like i first had.

EDIT: nevermind, i guess i can't edit it after so long. line 8 should have been:

```  for (stringpos=0; stringpos<strlen(binstring); stringpos++) {
```

This post has been edited by Mike Chambers: 15 October 2010 - 08:39 PM

### #7 teoh

Reputation: 0
• Posts: 12
• Joined: 08-October 10

## Re: how to convert binary to decimal using c programming

Posted 16 October 2010 - 11:05 PM

thank you so must...your explaination really help me...that make me understand..that want i want...in this want i can learn a lot...thank thank...

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }