type conversion

This is a discussion on type conversion within the C++ Programming forums, part of the General Programming Boards category; Hi, why doesn't this work? Code: #include <iostream> using namespace std; int main() { string abc = "12345abcde"; char une; ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Location
    Newcastle Australia
    Posts
    8

    type conversion

    Hi,


    why doesn't this work?

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    string abc = "12345abcde";
    char une;
    int uno;
    une=abc[1]; // une = "2"
    
    uno=(int) une; // uno=2
    uno *=3; // uno=6
    cout<<"uno is: "<<uno<<endl;
    //outputs "uno is 150"
    return 0;
    }
    why does c++ find it so difficult to
    reinterpret a character 2 from a string
    as a numeral 2 ?!

    thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The char type in C++ is just a small integer that holds character codes. So when you convert from char to int, you aren't converting from the numeric character to the number, you are converting from the numeric character's character code to a number (which isn't really a conversion at all, since they are both numbers).

    To convert a single character into it's numeric equivalent, simply subtract the character for zero: '0'. This works because the characters for all 10 digits are guaranteed to be in order.

    To convert a string to an int, you need to use a slightly more advanced method like a stringstream, atoi, or boost::lexical_cast.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    Which one of those methods to convert a character within an instance of the string class to an integer is the most efficient Daved?
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  4. #4
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    boost

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You should be asking which is the best to use design-wise. You might also ask if any have major efficiency issues if your program requires extra attention to speed or size.

    As far as best for design goes, I generally prefer to use the stringstream because I know how to test it for success or failure and configure it to convert the way I want it to, plus I use it for int to string conversions as well so there is the benefit of familiarity and consistency in the code. I would use boost lexical_cast if I was already using other boost libraries in my code. I have used atoi many times before in simple test code or C style code written by others that I was maintaining.

    Basically, any of the choices would work. I would guess atoi might be a tiny bit faster in practice, since it is lower level than the other two, but like I said that is rarely a valid concern even if it is true. I honestly don't know if any can be the cause of a major bottleneck, I've never noticed one.


    BTW, don't forget to #include <string>.
    Last edited by Daved; 01-10-2006 at 08:31 PM.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Location
    Newcastle Australia
    Posts
    8
    Thanks Daved,

    however,


    Code:
    une=abc[1]; // une = "2"
    
    uno=atoi(une);


    does not compile and gives the message
    "invalid type conversion from char to const char*.


    Also, how do I subtract 0 from 2 as you suggest?

    Thanks

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Just subtract '0' with the single quotes. A character within single quotes is treated as a character in the the code. So '0' is the character that represents zero, but 0 is the actual number 0. So to subtract the character zero, just subtract '0'.
    Code:
    uno = une - '0';// uno = 2
    The atoi function works with a C style string, which is an array of characters. That is why it didn't work when you passed in just the char variable. To get a C style character array from a string variable like abc, you would use the function c_str(). So with this code:
    Code:
    uno = atoi(abc.c_str()); // uno = 12345
    uno would be 12,345 because it converts the whole string to a number.

    You can use atoi if you want to convert the entire number at once, and use the subtract '0' technique if you want just a single digit.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Location
    Newcastle Australia
    Posts
    8
    ah ha I see, so 0 being ASCII 48 and 1,2,3 being 49,50,51 you just subtract 48 to convert from ASCII to fingers and toes. very good.

    atoi seems to be working now too. thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  2. type conversion
    By peterx in forum C Programming
    Replies: 3
    Last Post: 10-08-2005, 04:03 PM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. Windows using Dev-C++
    By Renegade in forum C++ Programming
    Replies: 15
    Last Post: 07-07-2005, 08:29 PM
  5. overloaded operator for type conversion error
    By mangoMan in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2004, 07:38 PM

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