this is exactly what im trying to do, and its probably an easy question to solve. Say i have a number in a variable (ie. 1) and i want to add a 0 to the one (ie. 10) how would i go by doing this?

I have to make a decimal to binary converter, so id need to now how to add a number to a number variable.

10-23-2007

brewbuck

Quote:

Originally Posted by bigmac(rexdale)

this is exactly what im trying to do, and its probably an easy question to solve. Say i have a number in a variable (ie. 1) and i want to add a 0 to the one (ie. 10) how would i go by doing this?

I have to make a decimal to binary converter, so id need to now how to add a number to a number variable.

Try to be more specific in your terminology. "Adding numbers" is accomplished with the addition operator "+". What you are trying to do is append a digit.

Your approach seems to be to hold the binary "value" as an integer whose decimal representation consists only of 0 and 1. That is definitely not going to work, because integer values have limited range and you are wasting most of it by working in the wrong base.

Numbers never have to be converted to/from specific bases while internally represented. A value is a value, not a sequence of digits. The digits come into being when the value is represented in generated output.

10-23-2007

bigmac(rexdale)

OK, well this is what I've been thinking of doing.

The program is Integer to Binary converter

I use the integer number to be converted, in a function that does something like this:

for loop(starting at 32, and descending, called counter)
number = number - 2^counter(number is the number the user wants to be outputted to look like the binary equivalent, counter would be 32, then 31 ...)

a if statement, where if number is less than 0, i add back what i substracted and the first digit of the binary output would be 0 (the variable name is b)

else if, number is more than 0 or eqal to, the first digit of the binary output would be 1(the variable name is b)

binary(a variable) would equal b

is if the first line is true, it would equal 0, if not, then 1, but:

then it does a next loop with counter being 31, so, how would the next digit (either a 0 or a 1) be appended? to what the digit was before.

ie. if it was 1 when counter equaled 32, when it equals 31 and the answer is one again, i need the binary variable to equal '11'

10-23-2007

jimzy

I'm not sure if I understand you correctly, but if you want to convert int to displayable binary, one quick approach would be to store remaining parts of division (which is either 1 or 0) in char array, eg.

int main (void) // see FAQ entry here
{
int number, bin, counter = 0; // you're starting accessing array at first index, which is 0
char bina[32]; // storing array was supposed to be char type, so it can be displayed as a string
printf("number ");
scanf("%d", &number);
while (number > 0 || number < 2147483647) // second condition isn't really needed
{ number = binar(number, &bin); // you need to modify original number that's checked in while loop
bina[counter] = bin + '0'; // you don't want to put 1 or 0 into array, but it's character equivalent in ascii codecounter = counter + 1; // increment counter once you're done with it's current value, not before
}bina[counter] = 0; // c-string should end with 0
printf("%d %s", number, bina);
}

Don't forget to reverse array before final displaying (because what you got now will display from bits from end - to - begining) - also watch out for terminating 0 when reversing, you don't want it at the very begining of your string.
Hope it's more clear now.

10-24-2007

bigmac(rexdale)

i fixed it up and reversed the arrays (i have a differen code from the last time though)

I just have a question now, i wrote so many lines of the same code (the arrays), would a for loop work so it wouldn't be so many lines of the same code

10-24-2007

dwks

Quote:

the number has to be between 0 to 2147483647

Yes, but an int can't hold a number larger than 2147483647. So checking for it is redundant. It's like saying, if the number is less than or equal to its largest possible number. It will always be true.

Quote:

I just have a question now, i wrote so many lines of the same code (the arrays), would a for loop work so it wouldn't be so many lines of the same code

Yes, definitely. I was going to suggest it myself. Use a for loop with something like

Code:

binary = binar(number, &bin);
bina[x] = binary;

in the body, with x looping from 0 to 31, inclusive (x=0, x<32). You have from 1 to 32 inclusive, but that's causing a buffer overrun.