CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    How to output special symbols in C++?

    For example, how to output "Delta" (like a triangle) in C++? Thanks.

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    Re: How to output special symbols in C++?

    If mean console output, you can start reading here: http://www.codeguru.com/forum/showthread.php?t=504805

    gg

  3. #3
    Join Date
    Jul 2005
    Posts
    1,030

    Re: How to output special symbols in C++?

    Thanks for the link. But would you show me a code example?
    Quote Originally Posted by Codeplug View Post
    If mean console output, you can start reading here: http://www.codeguru.com/forum/showthread.php?t=504805

    gg

  4. #4
    Join Date
    Nov 2003
    Posts
    1,902

    Re: How to output special symbols in C++?

    >> Thanks for the link.
    Try clicking it. Read that thread. Links that I posted in that thread should be clicked and read as well.

    Once you've read the information and tried the samples, ask any additional questions you may have here.

    gg

  5. #5
    Join Date
    Jan 2009
    Posts
    1,689

    Re: How to output special symbols in C++?

    C++11 added support for native UTF types.

    Code:
    std::u8string delta = u8"\u0394";
    Of course, if you want to see it in the terminal, the terminal has to support it. As far as I know the Windows terminal is ASCII, but almost all *nix terminals should output it correctly.

  6. #6
    Join Date
    Nov 2003
    Posts
    1,902

    Re: How to output special symbols in C++?

    std::u8string doesn't exist. u8"" string literals can be stored in char* strings or std::string.

    >> As far as I know the Windows terminal is ASCII
    No, it supports Windows codepages and direct Unicode output.

    >> , but almost all *nix terminals should output it correctly.
    Only if you set the user's locale, and the user's locale uses UTF8 as its narrow encoding.

    gg

  7. #7
    Join Date
    Jul 2005
    Posts
    1,030

    Re: How to output special symbols in C++?

    Okey, here is what I do in visual studio 2010,
    Code:
    InsertColumn(0, LPCTSTR(L"u0394"), LVCFMT_CENTER, 120);
    But the result is a small "u". How'd I display a delta(a small triangle)? Thanks.
    Quote Originally Posted by Codeplug View Post
    >> Thanks for the link.
    Try clicking it. Read that thread. Links that I posted in that thread should be clicked and read as well.

    Once you've read the information and tried the samples, ask any additional questions you may have here.

    gg

  8. #8
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: How to output special symbols in C++?

    Could it be you mean L"\x0394" rather than L"u0394"? Of course that only gains anything if the font you're using actually does support that character.

    Also, BTW, it's probably not such a good idea to cast a wide string literal into an LPCTSTR, at the latest when you switch to an MBCS build.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  9. #9
    Join Date
    Jul 2005
    Posts
    1,030

    Re: How to output special symbols in C++?

    I go to C/C++/Preprocessor/preprocessor definition in VS2010 to set up MBCS build. Is that the way to do?In my code, InsertColumn expects LPCTSTR. So if I pass L"\x0394", it won't even compile. How am I supposed to do that? Thanks.
    Quote Originally Posted by Eri523 View Post
    Could it be you mean L"\x0394" rather than L"u0394"? Of course that only gains anything if the font you're using actually does support that character.

    Also, BTW, it's probably not such a good idea to cast a wide string literal into an LPCTSTR, at the latest when you switch to an MBCS build.

  10. #10
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: How to output special symbols in C++?

    Whether to use a Unicode or MBCS build primarily depends on the requirements of your project. In general, nowadays it's recommended to use Unicode except when maintaining legacy projects. So if you have a choice, choose Unicode and save yourself some upcoming trouble up-front.

    Whether you can use special characters in an MBCS build depends on whether the code page in use supports the character. In that case you don't need to convert the character from Unicode to the target code page here since your character is a literal anyway. Just look up the code of the character in the target code page and use that. However, at least code page 1252 (Latin I), that is the default here and probably in most western countries does not in your case. And even if it did, this may be asking for trouble in case someone tries to run your program with a different code page set up.

    Another option is to use a symbol font instead. The font Symbol has the Δ as '\x44' (equivalent to the ASCII 'D'). I'm not certain right now, however, whether it's possible to use distinct fonts for the column headers in a single listview.

    The T in LPCTSTR means that its definition depends on the character set build option: It's a pointer to wide char in a Unicode build and to narrow char in an MBCS build. No wonder there's no implicit conversion from a wide string literal to LPCTSTR in an MBCS build, and casting between wide and narrow char pointers does no good, as you may have found out already.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  11. #11
    Join Date
    Jul 2005
    Posts
    1,030

    Re: How to output special symbols in C++?

    Here is how I try to display delta(small triangle) in the column of a list control,
    Code:
    TCHAR* s = new TCHAR[100];
    int len;
    		
    len = wcstombs(s, L"\x0394", 100);
    InsertColumn(0, s, LVCFMT_CENTER, 120);	
    delete[] s;
    But it doesn't work. Any guru here can point out what could be wrong? Thanks.
    Quote Originally Posted by Eri523 View Post
    Whether to use a Unicode or MBCS build primarily depends on the requirements of your project. In general, nowadays it's recommended to use Unicode except when maintaining legacy projects. So if you have a choice, choose Unicode and save yourself some upcoming trouble up-front.

    Whether you can use special characters in an MBCS build depends on whether the code page in use supports the character. In that case you don't need to convert the character from Unicode to the target code page here since your character is a literal anyway. Just look up the code of the character in the target code page and use that. However, at least code page 1252 (Latin I), that is the default here and probably in most western countries does not in your case. And even if it did, this may be asking for trouble in case someone tries to run your program with a different code page set up.

    Another option is to use a symbol font instead. The font Symbol has the Δ as '\x44' (equivalent to the ASCII 'D'). I'm not certain right now, however, whether it's possible to use distinct fonts for the column headers in a single listview.

    The T in LPCTSTR means that its definition depends on the character set build option: It's a pointer to wide char in a Unicode build and to narrow char in an MBCS build. No wonder there's no implicit conversion from a wide string literal to LPCTSTR in an MBCS build, and casting between wide and narrow char pointers does no good, as you may have found out already.

  12. #12
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: How to output special symbols in C++?

    Looks like the code page you use for your output doesn't support the Δ. And AFAICT the listview doesn't allow for individual fonts in the column headers, so using a symbol font in the header is no option either. So is there a chance you can turn your project into a Unicode one?

    There recently has been a discussion in the VC++ section about a possible way to use Unicode in controls from MBCS-built code. I don't recall how it turned out and IIRC it got rather hackish, but actually that may be your last resort in case migrating to a Unicode build is no option for you.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  13. #13
    Join Date
    Jul 2005
    Posts
    1,030

    Re: How to output special symbols in C++?

    Unfortunately, my project is NOT unicode enabled. But I tried to use #define _UNICODE in one file but it looks like I still can't use wide character in this particular file. I wonder if it is possible to define _UNICODE in this particular file only? Or another possible solution is that a unicode verison of InsertColumn is used instead. Is there such thing? Thanks.
    Quote Originally Posted by Eri523 View Post
    Looks like the code page you use for your output doesn't support the Δ. And AFAICT the listview doesn't allow for individual fonts in the column headers, so using a symbol font in the header is no option either. So is there a chance you can turn your project into a Unicode one?

    There recently has been a discussion in the VC++ section about a possible way to use Unicode in controls from MBCS-built code. I don't recall how it turned out and IIRC it got rather hackish, but actually that may be your last resort in case migrating to a Unicode build is no option for you.
    Last edited by LarryChen; February 3rd, 2012 at 06:33 PM.

  14. #14
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: How to output special symbols in C++?

    Quote Originally Posted by LarryChen View Post
    Unfortunately, my project is NOT unicode enabled. But I tried to use #define _UNICODE in one file but it looks like I still can't use wide character in this particular file. I wonder if it is possible to define _UNICODE in this particular file only?
    AFAICT it is not possible to set that up on a per-file bases in the VS IDE, though I can't think of any compelling reason why that shouldn't be done in any case. Make sure that when you #define _UNICODE in your source file, you do that before any includes because it controls how they behave. It may also be a good idea to #define UNICODE (without the leading underscore) as well; at least the VC++ 2010 IDE does that.

    If mixing MBCS and Unicode modules in a project should not be possible for reasons I don't know, what you can do at any rate is outsourcing some parts of your code into a DLL that is compiled as a Unicode build. That DLL would then need to expoose an MBCS-based interface, though, which requires you to consistently do conversions of any strings that are passed between the DLL and the main app. Writing such an interface layer has been discussed in the VC++ section quite a few times. Also, of course, you should make the DLL as self-contained as possible. But then again, if that's posible in a DLL, then why not in a static library or individual object file?

    Or another possible solution is that a unicode verison of InsertColumn is used instead. Is there such thing? Thanks.
    This is what the thread in the VC++ section I referred to in the second paragraph of my last post discusses. I don't recall the title of the thread, though, but I think it probably was in the last one or two weeks.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured