Unicode CString conversion to int
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Unicode CString conversion to int

  1. #1
    Join Date
    Jan 2008
    Location
    India
    Posts
    408

    Unicode CString conversion to int

    Hello, if I try to convert a CString object to int in Multibyte character set mode, I face no problem.
    Code:
    CString strNumber = _T("1234");
    int nNumber = atoi(strNumber.GetBuffer(strNumber.GetLength()));
    cout << nNumber <<endl;
    But if I go for unicode character set, I get the following error.
    error C2664: 'atoi' : cannot convert parameter 1 from 'unsigned short *' to 'const char *'
    How can I succeed with the conversion? Thanks.
    Rate the posts which you find useful

  2. #2
    Join Date
    Jul 2002
    Posts
    2,512

    Re: Unicode CString conversion to int

    Every string API has TCHAR version which compiles both for Multibyte and Unicode. You can find this in MSDN topic for the function. For atoi, this is _tstoi:

    int nNumber = _tstoi(strNumber.GetBuffer(strNumber.GetLength()));
    Last edited by Alex F; February 21st, 2009 at 02:45 AM.

  3. #3
    Join Date
    Jan 2008
    Location
    India
    Posts
    408

    Re: Unicode CString conversion to int

    Thanks. Let me look into it.
    Rate the posts which you find useful

  4. #4
    Join Date
    Feb 2009
    Posts
    34

    Re: Unicode CString conversion to int

    Hi! Im newbie in VisualC++ and this if my fisrt message...

    What's that "_t" we have to add? Is it only in VS 2008 ??


    I've been googling around but it doesn't manage "_t" properly.

  5. #5
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Smile Re: Unicode CString conversion to int

    That is a macro that get expanded to different things depending on whether UNICODE is defined.

    Take the example of atoi in this example.
    atoi is for the non-unicode version and _wtoi is for the unicode version.

    As Alex F mentioned, if you use _tstoi it would get expanded to either atoi or _wtoi depending on whether the build you are taking is a unicode build or not.

    The _T("string") macro is used for strings which gets expanded to L"string" for unicode and simply "string" for non-unicode builds.

  6. #6
    Join Date
    Feb 2009
    Posts
    34

    Re: Unicode CString conversion to int

    Thanks superman.

    When I read a .txt file (made by NOtepad) with CFile class and then print on screen what I find on file, there are usually bad characters at the end. It doesn't matter if I read 5, 10 or 20 bytes.

    Code:
            CFile f;
            f.Open(m_file, CFile::modeRead);
    	CHAR s[80];
    	int bytesread=f.Read(s,10);
    	f.Close();
    	m_out+=s;                 //m_out is "Cstring" linked to an EDIT CONTROL box)
    	UpdateData(FALSE);
    (Im making an MFC app).

    Should I declare "s" as unicode ? How is it ?

    Or should I avoid CFile commands?
    Last edited by Shadowrun; February 22nd, 2009 at 06:37 PM.

  7. #7
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Smile Re: Unicode CString conversion to int

    Declare as
    WCHAR s[80];

    If you want it to work for both unicode and non-unicode without a code change, declare as
    TCHAR s[80];

  8. #8
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Unicode CString conversion to int

    You're seeing "bad" characters because your character array is not NULL terminated. Just add the following to your code and you should be fine:
    Code:
            CFile f;
            f.Open(m_file, CFile::modeRead);
    	CHAR s[80];
    	int bytesread=f.Read(s,10);
    	f.Close();
    	s[10] = 0;    // NULL terminator - (the 10 is just the number of bytes you read).
    	m_out+=s;                 //m_out is "Cstring" linked to an EDIT CONTROL box)
    	UpdateData(FALSE);
    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center