# base conversion

This is a discussion on base conversion within the C Programming forums, part of the General Programming Boards category; Guys I need to write a program that will convert FROM and TO any bases ranging from 2 to 10. ...

1. ## base conversion

Guys I need to write a program that will convert FROM and TO any bases ranging from 2 to 10.
The program should prompt for the base “converted to” and the base “converted from”.
Can somebody tell me how I need to go about this and what aspects of C maybe helpful. I will do the actual coding and report any problems. Thanks

2. oh bases meaning binary, base 3 numbers, octal etc(up to 10)

3. Use % and /.

The rest of it is pretty easy.

Write soem code and show us, then you we can discuss it.

--
Mats

4. I guess one easy way to do so would be to convert the original number in "base 10" (and check if it's a valid number) and then reconvert it in the wanted base.

Let's say... you read the number in base 4 and want the answer in base 8.

Code:
``` 31234
=    <convert to base 10>
21910
=    <convert to base 8>
3338```
I know how to convert any numbers from any base to base 10, and to convert any numbers from base 10 to any base, but i don't know if it's possible to do it directly.

5. ## convert

How do I write a piece of code to convert from any base to base 10. I already figured out one to convert from base 10. eg
1234 in base 5 to base 10.How would that be implemented if that was inputed at the prompt?

6. All numbers have no base at all in the computer until you go to present them in text. So, if you want to go from base 4 to base 8, then you convert your input text from base 4 to "an integer". Then you present the same integer as base 8.

--
Mats

7. how can I implement a code that tests the amount of integers enterd and multiply that integer/s by powers starting at 0. eg 1234 in base 8 would be (1x8*3)+(2x8*2)+(3x8*1)+(4x8*0) (converted to base 10)
I am having problems on how to get the program to multiply by the right amount of integers and using the right amount of powers.
The idea is to convert any gien integer to base 10. Thats the only way I know.
Is there a shorter way?

8. There isn't any other way as far as the fundamental principle is [of course, the number isn't stored in any particular base if it's a number - only when it's a string does it have a base].

However, you can do this without knowing how many digits (and thus what power to apply) when you convert a number:
Code:
```char input[15];
char *s = input;
int n = 0;
fgets(input, sizeof(input), stdin);
while(*s && *s != '\n') {
char c = *s;
if (c >= '0' && c <= '9') {
n *= 10;
n += c - '0';
}
s++;
}```
This should be possible to convert to "any base" for input.

Output in any base should be similar but the other way around.

--
Mats

9. Modular arithmetic will do the trick for you.

Check this out:

http://en.wikipedia.org/wiki/Numeral_system

and especially the section called "change of radix."

10. also, try not to spam homework across multiple boards.
http://forums.devshed.com/c-programm...on-486649.html

11. Originally Posted by nattylife
also, try not to spam homework across multiple boards.
http://forums.devshed.com/c-programm...on-486649.html
Why not, you could get two contradictory [but perhaps individually correct] pieces of advice and end up with a complete hash of everything. [Of course, wasting other peoples time answering things that have been already answered better by someone else is also a good thing!]

--
Mats

12. Originally Posted by matsp
Why not, you could get two contradictory [but perhaps individually correct] pieces of advice and end up with a complete hash of everything. [Of course, wasting other peoples time answering things that have been already answered better by someone else is also a good thing!]

--
Mats
considering that both forums and many others will have many well equipped readers who are capable of answering questions, posting on 1 can have just a good chance of illiciting advice of many levels.

couple this with the fact that he didnt care to try to search for the answer himself and possibly find many, MANY sources and references to this type of homework question. rather he would have the answer come to him.

13. Hey, another devshed regular joins the fold

> considering that both forums and many others will have many well equipped readers
> who are capable of answering questions.
Or, on realising that they've posted all over the web, post a link to urgent and then ignore them in all forums for being such a self-centred "me me me" poster.

> Why not, you could get two contradictory [but perhaps individually correct] pieces of
> advice and end up with a complete hash of everything.
In my evil moments, I've considered doing this myself just to play head-games with them

The ones you have to watch out for are those lazy so-and-so's who get a bit of an answer from forum 1, then post that as "this is what I've done" on forum 2. They get a bit more from forum 2, then post the whole lot back to forum 1 with apparent "progress". I've seen it happen a couple of times, and it ain't pretty.