Thread: a C++ source code..

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    4

    a C++ source code..

    Hi there ..

    i need a source code for a program written in C++ to convert a number in base b to a number in base c, while both b and c can be equal to either 2, 8, 10, or 16.

    i'll really appreciate it if anyone can help

    & thanX

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    itoa(i,buffer,base)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    Thumbs up

    Quote Originally Posted by Nora
    Hi there ..

    i need a source code for a program written in C++ to convert a number in base b to a number in base c, while both b and c can be equal to either 2, 8, 10, or 16.

    i'll really appreciate it if anyone can help

    & thanX
    Here i have attached the complete code for the same..Enjoy
    Last edited by Salem; 12-09-2006 at 08:45 AM. Reason: Remove attachment

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Here i have attached the complete code for the same..Enjoy
    If the OP has not attmpted to solve the problem, please do not write the entire code
    for them. It really does go against the learning process
    Double Helix STL

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    Unhappy

    Quote Originally Posted by swgh
    If the OP has not attmpted to solve the problem, please do not write the entire code
    for them. It really does go against the learning process
    Oh, i am sorry if it was against the rules. Mods can delete it.....Sorry again

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    itoa (ugh), strtol or *scanf or *printf (old), or stringstreams would work. Even regular old math would work.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    Hi again..

    i'm just a begginer in C++ .. and i have the problem i mentioned before as a project..
    i've tryed to solve it .. and i have some questions..

    is it right if do the following:
    cout>>"Please enter the number you want to convert followed by its base";
    cin<<num,base;
    then i put an "if"statement .. if the base is not equal to 10 then it will enter a "for" loop to convert it it to a decimal number..then another loop to convert this decimal to the base that the user choose after that..

    this is for a start is it right or i have to think about something else? cuz i need to know so that i can go on with the rest of the code..

    that's it.. thank you all for ur replys

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you start with a very simple program which
    - prompts for input (you've had a go at that)
    - reads input (you've had a go at that as well)
    - prints what was input.

    When that's working, move onto the next small step of the program, or if you're still stuck, post the code you have so far.
    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.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    Hi all ...

    i getting really tired by this source code! i have asked our TA about it.. and he said u need to use strings in order to make it easier for you .. but we didn't learn strings at all!

    so its becaming really complicated :s i got the source where i convert from decimal to any base .. i just need the source that convert from any base to decimal... any help from anyone would be really helpful cuz i'm really desperate :/

  10. #10
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131
    Quote Originally Posted by Nora
    Hi all ...

    i getting really tired by this source code! i have asked our TA about it.. and he said u need to use strings in order to make it easier for you .. but we didn't learn strings at all!

    so its becaming really complicated :s i got the source where i convert from decimal to any base .. i just need the source that convert from any base to decimal... any help from anyone would be really helpful cuz i'm really desperate :/
    Code:
    Base 2(binary) -> Base 10(decimal)
    100101         -> 1*2^0 + 0*2^1 + 1*2^2 + 0*2^3 + 0*2^4 + 1*2^5 = 1 + 0 + 4 + 0 + 0 + 32 = 37
    
    Base 8(oct) -> Base 10(decimal)
    720123      -> 3*8^0 + 2*8^1 + 1*8^2 + 0*8^3 + 2*8^4 + 7*8^5 = 237 651
    
    Generally. By converting from base X to base 10, you have to use this technique
    
    Let's say A is an array (0-based index - meaning that the array's indexes are 0 1 2 3 ...)
    A = { 1, 0, 1, 0, 0, 1 }	//The numbers from the 1st example - except they are written backwards, 
    so as not to mess up those indexes. You have to read the indexes from right to left and then number 
    them 0 1 2 3 and so on, but in C and C++, arrays start with index 0, thus if you would write the numbers 
    in the same order that you see them in the 1st example, the indexes would be reversed.
    
    then you have to add them together using this formula:
    
    A[i]*X^i + A[i + 1]*X^(i + 1) .. A[the number of elements in the array A - 1]*X^(the number of elements in the array A - 1)
    
    If you still don't get it, use Google!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seven Kingdoms I: Ancient Adversaries for Linux
    By MIH1406 in forum Projects and Job Recruitment
    Replies: 13
    Last Post: 01-17-2010, 05:03 PM
  2. How do you call another source code file?
    By nifear4 in forum C Programming
    Replies: 2
    Last Post: 10-28-2008, 12:16 PM
  3. DxEngine source code
    By Sang-drax in forum Game Programming
    Replies: 5
    Last Post: 06-26-2003, 05:50 PM
  4. Lines from Unix's source code have been copied into the heart of Linux????
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 05-19-2003, 03:50 PM
  5. Source Code Beautifier
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-05-2002, 09:21 PM