Thread: Does function strtol work??

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Does function strtol work??

    I was wondering if function strtol() works. I was expecting the value of "int i" to be 115 after the call of strtol, since that is the decimal ASCII value of char 's', but instead it outputs 0:

    C++ code - 30 lines - codepad

    Why is that?

    EDIT: Hmm...maybe function atoi() is what I should be using instead.

    EDIT again: No, same problem with that too...
    http://codepad.org/j3LnkJ48
    Last edited by Programmer_P; 07-17-2010 at 07:14 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Let me ask another question: why do you care about what number 's' turns into?

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    "s" is not a valid string representation of a number. strtol is a format-knowing function, and that is all. If you used "115", you would get the int = 115.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by CodeMonkey View Post
    "s" is not a valid string representation of a number. strtol is a format-knowing function, and that is all. If you used "115", you would get the int = 115.
    Ok, well I need a function which can take a char and convert it to its ASCII representation.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    Let me ask another question: why do you care about what number 's' turns into?
    Because it is valid for enumerators of an enum to be given char values (for some bizarre reason...), and so I need to get the underlying numerical values.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    ...What?

    You mean you need an ASCII table?

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    A char is its ascii representation. It's only a matter of how your functions interpret the data. You can cast:
    Code:
    #include <iostream>
    
    int main()
    {
       char letter = 's';
       std::cout << letter << " is ascii " << static_cast<int>(letter) << std::endl;
       return 0;
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #8
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    ...What?

    You mean you need an ASCII table?
    No, that's not what I mean. Obviously I already know about the ASCII tables on the net.
    I don't actually care about what the ASCII values are. I could care less really. I just need my program to know, is all.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Programmer_P View Post
    I just need my program to know, is all.
    You're not going to explain why this matters are you?

    Because trust me, the computer, and by extension everything you use, knows.

  10. #10
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Oh that. Yeah, that wants to be an associative array, hash table, whatever you want to call it.

  12. #12
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    You're not going to explain why this matters are you?

    Because trust me, the computer, and by extension everything you use, knows.
    I already did, but you apparently missed the post.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  13. #13
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by CodeMonkey View Post
    A char is its ascii representation. It's only a matter of how your functions interpret the data. You can cast:
    Code:
    #include <iostream>
    
    int main()
    {
       char letter = 's';
       std::cout << letter << " is ascii " << static_cast<int>(letter) << std::endl;
       return 0;
    }
    Cool. Could I also cast a std::string or a c string to a int?
    Trying to do that now...
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  14. #14
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Maybe I'm being stubborn, but I think that I have solved your problem and explained the underlying misconception. What are you trying to do with this code?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  15. #15
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by CodeMonkey View Post
    A char is its ascii representation. It's only a matter of how your functions interpret the data. You can cast:
    Code:
    #include <iostream>
    
    int main()
    {
       char letter = 's';
       std::cout << letter << " is ascii " << static_cast<int>(letter) << std::endl;
       return 0;
    }
    Cool. Could I also cast a std::string or a c string to a int?
    Trying to do that now...

    EDIT: Nevermind, that wont work. I'll just use the first char of the string instead.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM