Thread: convert from int to cstring

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    74

    convert from int to cstring

    hi all,

    how to convert int into cstring? i code this:

    <code>
    CString count;
    count.Format("%d", resultCount);
    </code>
    where resultCount is integer
    error message :
    錯誤 1 error C2664: 'void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [3]' to 'const wchar_t *'

    the coding seems to be correct.
    what's wrong? thanks very much!

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    CString count;
    count.Format(L"&#37;d",resultCount);
    That should fix it. The L specifies you are creating a wchar string or wide character string. To remove this annoyance there are macros like TEXT that convert to a wchar. You can also turn Unicode off in your project options. Not wise if you are planning to distribute overseas or use international (non-ASCII) strings but unless you are it's just as simple to shut it off.

    With Unicode on CString's are by default wchar.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    CString count;
    count.Format(_T("%d"), resultCount);
    Preferred way to write a Win32/Linux/Winny program.
    Good practice since you'll learn to wrap your text in _T when you really do need to use unicode. It makes it so your app can simply be set to unicode and recompiled and tada! It works, without any modifications!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Preferred way to write a Win32/Linux/Winny program.
    What do the Win32-specifc ATL/MFC CString and the Microsoft-specific _T macro have to do with Linux?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What I meant was a program with a real GUI. Ya know, a real application. Even if you scrap MFC later, you can still use _T to make it unicode safe and port it between unicode/ansi at any time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I don't know what a GUI has to do with it. Also, I think you mean ASCII, not ansi, though it might be safer to simply say "Multi-byte character set".

    _T("") is certainly more flexable than simply always using L"". One can still easily define the _T macro on other platforms where necessary.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Bubba View Post
    You can also turn Unicode off in your project options. Not wise if you are planning to distribute overseas or use international (non-ASCII) strings but unless you are it's just as simple to shut it off.

    With Unicode on CString's are by default wchar.
    I get extremely frustrated at programmers who don't support Unicode, it's not just people overseas who use other languages at times, or who might potentially have things like filenames with non-ANSI characters. It's really annoying when a program refuses to open a file because it doesn't like the filename.

    If you're doing Win32, I think the least you can do is support the native character set of your operating system, and allow the user to open any legal filename.

    So I think the best option here is to help him learn how to do this in Unicode and save the end-users of his app the frustration.
    Last edited by Cat; 12-06-2007 at 02:05 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Elysia View Post
    What I meant was a program with a real GUI. Ya know, a real application. Even if you scrap MFC later, you can still use _T to make it unicode safe and port it between unicode/ansi at any time.
    That's assuming the API you port to has the same switching mechanism as the Win32 API. Which is not true. GTK+ strictly uses UTF-8 throughout. Qt uses UTF-16 exclusively. Among the big, free GUI frameworks, I think only wxWidgets has the switching mechanism.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well too bad! Then write a little macro that transforms _T into a function that returns a UTF-8 string, or maybe two macros, _T8 and _T16 to transform to ASCII/UTF8/16 respectively.
    I find it silly that they use different types of Unicode and can't support the other.
    And unicode safe refers to that you can simply recompile your project w/o changing any code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Would you be very annoyed if I went on to mention that _T is reserved for the implementation, so strictly speaking, you're not allowed to emulate it if your particular implementation (read: the compiler) doesn't have it?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    UTF-8 is backwards compatible to ASCII so there is no need for a macro. That's why it's such a popular encoding.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Does it matter? If it works, it works.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Does it matter? If it works, it works.
    The "unsafe" stuff in your sig works too...

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't see a relation to compiler macros and keywords to unsafe functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    The attitude of "it works for me" is what causes people to get into the realm of undefined behavior simply because it appears to work for them or their compiler allows it, etc. etc..

    If _T() is defined by your compiler to do something else, then you can't use it that way. If you have that attitude, however, you'll use it anyway just because one compiler supports it and end up with code that possibly doesn't work. Is it likely? Perhaps not. It's also probably not likely that using void main() will blow up your computer. In the end, if a compiler supports void main(), why should you not use it? Portability perhaps?

    If you are trying to write portable code, imo, I suppose you should probably provide your own macro for it. If you want, even just have your macro call _T() for compilers that you know support it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM