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. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    7

    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. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    oh bases meaning binary, base 3 numbers, octal etc(up to 10)

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Use % and /.

    The rest of it is pretty easy.

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

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    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.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    7

    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?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    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?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    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."

  11. #11
    Registered User
    Join Date
    Jun 2006
    Location
    Plantation, FL
    Posts
    3
    also, try not to spam homework across multiple boards.
    http://forums.devshed.com/c-programm...on-486649.html

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by nattylife View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Jun 2006
    Location
    Plantation, FL
    Posts
    3
    Quote Originally Posted by matsp View Post
    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.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. base 10 to base 2 conversion
    By dteum in forum C++ Programming
    Replies: 4
    Last Post: 09-27-2008, 02:12 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21