Special Characters

This is a discussion on Special Characters within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I was wondering if it were possible to output special characters to a static control or another control. For ...

  1. #1
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183

    Special Characters

    Hi,

    I was wondering if it were possible to output special characters to a static control or another control. For example, the square root symbol (√), which is character # 251, shows up just fine in the DOS window. But when I try to set that to a static control, it doesn't work (it shows a weird character). Does anyone know how to do this? Thanks. I am using Visual C++ 2008 Express Edition.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    DOS and Windows uses different character sets.
    Why not try
    Code:
    wchar_t* str = "√";
    // Set window text using str here
    ?
    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.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    You also have to consider what font the control is using. Do Start->Run "charmap.exe". Use that app to inspect the glyphs used by a font and choose one for your control to use. If you register the class or create the window, use RegisterClassW() and CreateWindowW(). When you send it the WM_SETTEXT method, use SendMessageW() and use a wide character string. I checked a few fonts and the Unicode character for square root seems to be 0x221a. Here's how you could use it in a string literal:
    Code:
    const wchar_t *p = L"The \x221a of 4 is 2";
    gg

  4. #4
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

  5. #5
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Hey thanks everyone, that worked. The only problem is that I would like to combine it with an std::string, and I don't know how to do that. Can anyone help me? Thanks.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    Code:
    typedef basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > wstring;
    wstring my_str = L"The \x221a of 4 is 2";
    my_str = L"√";
    That should do it, I believe.
    Last edited by Elysia; 12-09-2007 at 08:48 AM.
    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.

  7. #7
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Uhhh... Elysia that gives me a couple of errors, such as:

    Code:
    1>c:\documents and settings\michael (2)\my documents\visual studio 2008\projects\formula helper\main.cpp(125) : error C2872: 'wstring' : ambiguous symbol
    1>        could be 'c:\documents and settings\michael (2)\my documents\visual studio 2008\projects\formula helper\formula helper.h(17) : std::basic_string<_Elem,_Traits,_Ax> wstring'
    1>        with
    1>        [
    1>            _Elem=wchar_t,
    1>            _Traits=std::char_traits<wchar_t>,
    1>            _Ax=std::allocator<wchar_t>
    1>        ]
    1>        or       'c:\program files\microsoft visual studio 9.0\vc\include\xstring(2212) : std::wstring'
    1>c:\documents and settings\michael (2)\my documents\visual studio 2008\projects\formula helper\main.cpp(126) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const char [2]' (or there is no acceptable conversion)
    1>        c:\program files\microsoft visual studio 9.0\vc\include\xstring(914): could be 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const std::basic_string<_Elem,_Traits,_Ax> &)'
    1>        with
    1>        [
    1>            _Elem=wchar_t,
    1>            _Traits=std::char_traits<wchar_t>,
    1>            _Ax=std::allocator<wchar_t>
    1>        ]
    1>        c:\program files\microsoft visual studio 9.0\vc\include\xstring(919): or       'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const _Elem *)'
    1>        with
    1>        [
    1>            _Elem=wchar_t,
    1>            _Traits=std::char_traits<wchar_t>,
    1>            _Ax=std::allocator<wchar_t>
    1>        ]
    1>        c:\program files\microsoft visual studio 9.0\vc\include\xstring(924): or       'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(_Elem)'
    1>        with
    1>        [
    1>            _Elem=wchar_t,
    1>            _Traits=std::char_traits<wchar_t>,
    1>            _Ax=std::allocator<wchar_t>
    1>        ]
    1>        while trying to match the argument list '(wstring, const char [2])'
    1>c:\documents and settings\michael (2)\my documents\visual studio 2008\projects\formula helper\main.cpp(129) : error C2440: 'type cast' : cannot convert from 'wstring' to 'LPARAM'
    1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    I don't know what I did wrong, I defined "wstring" in my header file, and then tried to use it in my code. Did I do something stupid?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    Oops, forgot L before the last string.
    If it doesn't compile with the fixed code, why not show what line the compile errors occur on?
    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.

  9. #9
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    The lines were the errors occur are:
    Code:
    wstring my_str = L"The \x221a of 4 is 2";
    my_str = L"√";
    SendMessageW(GetDlgItem(hwnd, IDC_RADDIRECTIONS), WM_SETTEXT, 0, (LPARAM)(my_str));

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Also, the wstring typedef already exists in the standard library. No need to redefine it. (That's the ambiguity error.)
    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 mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Ok, I removed the place where I defined wstring, but know it won't let me convert from "wstring" to "LPARAM" in the SendMessageW function. Anyway, though, how would using a wstring allow me to use std::strings with them?

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    Do my_str.c_str() to get the buffer and pass it to SendMessageW.
    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 mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Thank you Elysia, that solved the SendMessage problem. However, I still don't know how to use it with an std::string. Here's what I tried:
    Code:
    	wstring my_str;
    string str = " test";
    my_str = L"√";
    strcat((char *)my_str.c_str(), str.c_str());
    SendMessageW(GetDlgItem(hwnd, IDC_RADDIRECTIONS), WM_SETTEXT, 0, (LPARAM)my_str.c_str());
    However, it gives me an output that looks like this: "√琠獥t쳌쳌쳌쳌". Does anyone know what's wrong, or a better way? I believe it's because in strcat I converted it to a char *, but maybe not.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    wstring IS string.
    string holds char.
    wstring holds wchar.
    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
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Ok, I get it now. However, is there any possible way to use an std::string in conjunction with a wstring? I could always just set the text with the wstring, and then set the std::string on the end of that, but it just erases what ever was there first. Is there a WM_APPENDTEXT message or something like that?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. Bubblesort with special characters
    By Zarniwoop in forum C Programming
    Replies: 3
    Last Post: 05-03-2008, 10:43 PM
  3. special characters removing from srting
    By cnu_sree in forum C Programming
    Replies: 5
    Last Post: 06-06-2007, 08:30 PM
  4. special characters
    By volk in forum C Programming
    Replies: 8
    Last Post: 02-20-2003, 02:57 AM
  5. Special (non) Keyboard Characters
    By TechWins in forum C++ Programming
    Replies: 3
    Last Post: 05-01-2002, 12:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21