6 Replies - 10001 Views - Last Post: 16 October 2010 - 11:05 PM Rate Topic: -----

#1 teoh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • 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
Was This Post Helpful? 1
  • +
  • -

#3 Mike Chambers  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • 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;
}


Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,151
  • 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. :)
Was This Post Helpful? 1
  • +
  • -

#5 Mike Chambers  Icon User is offline

  • D.I.C Head

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

Re: how to convert binary to decimal using c programming

Posted 15 October 2010 - 07:59 PM

View Postmacosxnerd101, 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

Was This Post Helpful? 0
  • +
  • -

#6 Mike Chambers  Icon User is offline

  • D.I.C Head

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

Re: how to convert binary to decimal using c programming

Posted 15 October 2010 - 08:26 PM

View Postteoh, 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

Was This Post Helpful? 4
  • +
  • -

#7 teoh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1