Thread: Change data format from CString to int

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    68

    Cool Change data format from CString to int

    If I have this below CString data and I want to change it format to int.

    Example:
    CString data_str = "1487";
    int data_int;

    I want

    data_int = 1487

    Do you know how to do that?

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Just use one of the standard C funcitons like: sscanf, atoi, strtol...

    CString data_str = "1487";
    int data_int = atoi(data_str);

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Monster
    Just use one of the standard C funcitons like: sscanf, atoi, strtol...

    CString data_str = "1487";
    int data_int = atoi(data_str);
    One way, but beware that the std lib interpretations are not Unicode aware.....so as the rest of MFC can switch between Unicode & ANSI easilly, all your code should be able to do this too.

    Try _ttoi which is defined in tchar.h (part of a normal MFC build nayway, so no extra headers).....

    If its unicode, _ttoi goes to _wtoi (M$'s Unicode version of atoi). If not, then plain old atoi is used

    Code:
    	int x = 1487;//Pick an int
    	CString data_str;//Creat a string
    	data_str.Format(_T("%d"),x);//make int into string
    	MessageBox(data_str);//display
    
    	int y = _ttoi(data_str);//string to int
    	y += 1000;//change int
    	data_str.Format(_T("%d"),y);//back to string
    	MessageBox(data_str);//display

  4. #4
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    How does CString::getBuffer() work for this? Doesn't it return a const char* to the char array? If I shouldn't be using it why not?
    Couldn't think of anything interesting, cool or funny - sorry.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I believe atoi() and related functions require null terminated array/Cstyle string and will not work with instances of string classes.

    Most string classes have some function to return the underlying null terminated char array/Cstyle string. In some classes it's c_str() but I don't use CStrings so I can't say for sure what it is there. In fact many classes have methods that convert the string to an int for you. I know of several classes where it it ToInt(), but again I don't use CString so I can't say whether there is such a conversion function or not. The syntax

    Cstring sInput = "1234";
    int iInput = atoi(sInput.c_str());//or ttoi() or whatever
    iInput = sInput.ToInt();

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by elad
    I believe atoi() and related functions require null terminated array/Cstyle string and will not work with instances of string classes.

    Most string classes have some function to return the underlying null terminated char array/Cstyle string. In some classes it's c_str() but I don't use CStrings so I can't say for sure what it is there. In fact many classes have methods that convert the string to an int for you. I know of several classes where it it ToInt(), but again I don't use CString so I can't say whether there is such a conversion function or not. The syntax

    Cstring sInput = "1234";
    int iInput = atoi(sInput.c_str());//or ttoi() or whatever
    iInput = sInput.ToInt();
    Very true in std::string....but not so in CString....there is a conversion operator that allows implicit conversion to LPTSTR or whatever.....

    Not a very good practice as far as I know as implicit conversions can lead to problems, and according to my C++ texts, that's why the std::string doesnt support this facility...the great and the good decided that it was far to error prone and so std::string::c_str() was brought in to allow explicit conversion

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Switch/case Problems (long code in post)
    By Wraithan in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2005, 06:40 PM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM