Thread: number system conversion

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    18

    number system conversion

    i need to build a program to convert

    Binary-to-Octal, Octal-to-Binary.
    Binary-to-Hexadecimal, Hexadecimal-to-Binary.
    Decimal to Binary and Hexadecimal.
    Binary, Hexadecimal, Octal to decimal.

    i have no heads or tail how to do this.. I'm thinking of doing a switch case for the user to choose which conversion to choose.. but i have no idea how to create the formula for the coversion.. for example, to change octal to binary, i may use remainder(%).. but how can i arrange it from down upwards?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you do those conversions by hand (pen and paper type thing)?

    If so, you should be able to write down each step in your own language, and then write some C code that performs those steps.

    Yes, the modulus (%) operator will come in handy.

    --
    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.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    yes.. but for example, changing octal to binary, i'll be dividing the numbers in ocatl with the number 2.. The remainder will be taken as the answer.. But it will be taken from down to up.. If i'm doing on paper, i can see it.. but in c programming how can i ask the program tu read it from bottom to up?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, you may have to "reverse" the number after you generated it, perhaps, or start storing at the back of the array, perhaps?

    Also remember that if you read in a number (as an int, rather than as a string), it is always stored in binary form in the computer, no matter what base it was when you read it in.

    --
    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.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    Quote Originally Posted by matsp View Post
    Well, you may have to "reverse" the number after you generated it, perhaps, or start storing at the back of the array, perhaps?

    Also remember that if you read in a number (as an int, rather than as a string), it is always stored in binary form in the computer, no matter what base it was when you read it in.

    --
    Mats
    i don't quite understand.. i intended to make the input an integer only.. do you mean that the computer will display a binary no matter what?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,926
    No, what is displayed is the representation of the number in a given base. Internally, the computer stores and manipulates a binary representation of the number.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    After using a switch to ask the user what type conversion he wants, you can just input the number using scanf() as hexadecimal, octal, or decimal. Then you can output the number using printf() in the system you want.
    Your only problem is that scanf() and printf() can't input/output in binary. But as matsp pointed out, don't forget that the computer stores all data in binary regardless of the method of input. So use that (It shouldn't be that hard)...

    EDIT: sounds like you want to do the conversions yourself. First you have to learn how to do it manually (you can search Google for that). Then - like matsp said - write some C code that'll do that. Give it a try and we'll help you out if you can't get it.
    Last edited by Abda92; 04-09-2008 at 07:23 AM.
    I might not be a pro, but I'm usually right

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    thanks for all your help.. I'll give it a try..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Airline Booking System - PLZ HELP
    By Gecko2099 in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 12:17 PM
  5. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM