Hi there Daniweb,

right now im in an engineering program and we are being put through a crash course in C. Ive never dont any type of programming before so it is quite an adventure (to say the least).

Currently we are doing an assignment in which we have to convert a binary number (base 2) to a decimal number (base 10). I think I have the beginnings of the program, but Im not sure how to proceed. Im not sure what variables i should be assigning and how to implement them.

we were given pseudo code to implement into the program, but i think im missing a lot.

Pseudo-code algorithm: (start by convincing yourself that this algorithm works)

1. Prompt the user to enter a binary number, followed by the Enter key

2. Assign variable decimalValue 0

3. Get the first binary digit and Assign variable nextDigit the value of the digit

4. While (there are still digits to be processed)

4.1 Assign variable decimalValue the value of (decimalValue * 2) + nextDigit

4.2 Get the next binary digit and Assign nextDigit the value of the digit

5. Output the value of variable decimalValue

so far i have this:

#include <stdio.h>
#include <stdlib.h>

int decimalValue; 
int binnumber;

int main()
{
   printf("Please enter a binary number: \n");
  
   decimalValue = 0;
 
   scanf("%i", &binnumber);
   
   
  
  system("PAUSE");	
  return 0;
}

should my int binnumber actually be nextDigit (following the pseudo code)?

could someone please help guide me to the next step? i THINK i should be using an 'if' or "while" statement. i know that the program should terminate when the value returned = 0. i also know that in the calculations i need to have something along the lines of "x %10 = a" followed by " a/10 = b" (something along those lines, i used completely random variable letters there). my friend in the class said something about converting an int into a char, but i wasnt sure where he was going with that.lol

with regards to the binary numbers, how many letters should i assign? couldnt someone be difficult and enter a 10 digit binary? in that case, should i have 10 letters (one for each integer) for the binary to decimal function?

i know this must sound absolutely stupid, but this is a very foreign language to me (C, not english.lol) and we have only been taking the course for a few weeks. i cant stand how in the textbook all their examples are so simple! why cant they give harder examples!?lol

i really appreciate any help you can give me. im quite concerned about this as i feel like im only taking in small chunks of what i should be.

ill update the code portion of my post as i proceed. hopefully i dont make a complete arse of myself!

thanks in advance

DFE

thank you for that link :D

in the link, would the "unsigned int value" be my "int decimalValue"?

also, what does this represent? is it necessary? or is it just examples of binary numbers?

#
static const char *binary[] =
#
{
#
"0000","0001","0010","0011","0100","0101","0110","0111",
#
"1000","1001","1010","1011","1100","1101","1110","1111",
#
"1010010110100101", "11011110101011011011111011101111",

unsigned int value would be your decimal value.

The static const char *binary is a list of binary values that are being converted to their decimal equivalent. In your case you are getting this input from the user, so you don't need it.

Also since a user could enter a really long binary value which you will probably not be able to hold in an integer, it makes more sense to store your binary value as a char* as shown in the program.

>in the link, would the "unsigned int value" be my "int decimalValue"?
Unsigned are positive numbers only.

>also, what does this represent? is it necessary? or is it just examples of binary numbers?
Those are just some example of binary numbers for test purpose. The author implemented them in an array of strings.

[Edit:]The important part for you to understand it is the brief explanation before the example. If you get that, you'll be able to find your own way of doing it.

is the const part of the char declaration necessary? i havent seen it utilized before.

so basically this is an extremely easy program, but i just suck at C?lol


in regards to this section of the code. what does size_t i; do? is that just relating to the size of the text? im not 100% sure what it is doing. i see that it declares i as something (as it wasnt used earlier in the program) but i dont know why.

size_t i;

for ( i = 0; i < sizeof binary / sizeof *binary; ++i )

{

unsigned int value = btou(binary[i]);

printf("btou(\"%s\") = 0x%X = %u\n", binary[i], value, value);

The const means that you cannot modify the contents of the char.

In your case, since you are asking the user to input the binary value, so you do not want to make it a const , since you will be modifying it to store your value.

Make sure you allocate enough space to the array though .. like char myBinstring

we havent used strings yet in our classwork. are there other ways to approach it?

i think our prof expects us to use modulus functions. such as:

x%10 = y
y/10 = z

then use a loop or something or other to continue through all the binary integers until it ends up being zero.

holy crap. im so bad at debugging.lol

this is going to take a LOOONG time to complete.lol. im trying to use the example as a pseudo-template, but im having zero luck. i think the way that the code writer for the example is doing it isnt the same way that our prof expected (as i said earlier), so i dont want to use these types of functions that we havent even used yet.lol

Modulo will work. If you are using an int to store your binary value make sure that you check the upper limits of the maximum integer value. If you want to be able to store larger values, then you could use a long or a long long.

Doing it a long hard way here is a brief explanation.

If you have a integer input then you first need to split it into indervidual numbers i.e

11010111

would become

1 1 0 1 0 1 1 1

(spaces show a split into an array or whatever)

Then you need to multiply each one by 2^x where x increase by one(starting at 0), however this must be done in reverse order. then you must add them together to give you your decimal number

1x2^7 + 1x2^6 + 0x2^5 + 1x2^4 + 0x2^3 + 1x2^2 + 1x2^1 + 1x2^0
----------------------------------------

As for splitting an integer there are multiple ways, some involve convering it to strings. However going on the idea of normal and modulus division. This is how it would be done

ie the number 11010111
x = (number of chars)

(11010111 % x) / x-1

that would get you the ver far right 1

then next would be
(11010111 % x-1) / x-2
etc

hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.