Thread: casting a string of chars as a string of ints

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    Question casting a string of chars as a string of ints

    For a string text,

    why isn't

    for(i = 0; strlen = n, i < n; i++)
    text[i] = (int) text[i]

    casting each character of text as an integer?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Presumably text is an array of chars, so of course it remains an array of chars. Perhaps you want to copy over each char in text to an int in an array of ints.
    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

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    Arrow

    How do I do that?

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    int nums[10];
    char str[10];
    
    for ( int i = 0; i < 10; ++i )
    {
        nums[i] = str[i];
    }
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    don't forget the (int) cast

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Epy View Post
    don't forget the (int) cast
    Why would you need one? I'm assigning a 1 byte int to a larger int, so no cast is needed.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. Casting a vector to a string
    By Hikaru in forum C++ Programming
    Replies: 10
    Last Post: 08-21-2006, 07:55 PM
  3. Replies: 8
    Last Post: 03-31-2006, 08:15 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM