Number representation

This is a discussion on Number representation within the C++ Programming forums, part of the General Programming Boards category; Hello. I would like to put a question maybe someone could help me. I wanna create a c++ application that ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    19

    Talking Number representation

    Hello.

    I would like to put a question maybe someone could help me. I wanna create a c++ application that get's a number as a argument and does stuff with it.
    Using other commandline application's I observed that most application interpret a number writen like 10 as 10 in decimal and a number as 0x10 or 0X10 as 16 in decimal/ 10 in hex.

    I would like to ask if somebody do know about a library function that convert's a string, representing a number into a numerig value, taking care of the common numbering representations. I search for something like atoi() or sscanf(s,"%d") but where I don't have to specify if the string is representing a hex number or dec number .

    Do you also know some document where the different numbering representation for command line application's are described ?

    Thanks for your time

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    strtol() and sscanf("%i") both treat strings in the following way

    Begins with 0x, it's hex
    Begins with 0, it's octal
    Begins with 1..9, it's decimal
    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.

  3. #3
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    edit: on second thought I probably misread your question but I'll share this anyway

    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    
    int main()
    {
        std::string num("100");
    
        std::istringstream iss_dec(num);
        std::istringstream iss_hex(num);
        std::istringstream iss_oct(num);
    
        int dec;
        iss_dec >> std::dec >> dec;
    
        int hex;
        iss_hex >> std::hex >> hex;
    
        int oct;
        iss_oct >> std::oct >> oct;
    
        std::cout << num << " interpreted as decimal: " << dec << std::endl;
        std::cout << num << " interpreted as hexidecimal: " << hex << std::endl;
        std::cout << num << " interpreted as octal: " << oct << std::endl;
    
        return 0;
    }
    Last edited by BMJ; 10-29-2010 at 01:39 PM.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    19

    Thumbs up

    Thank you.

    sscanf("%i") is what I was searching.

    Also the conversions using C++ methods where helpfull too. I didn't knew them till now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number user input
    By jimtuv in forum C Programming
    Replies: 62
    Last Post: 06-13-2010, 11:08 AM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

Tags for this Thread


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