Thread: How one can convert wchar_t to char in C? Please suggest the code.

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    61

    How one can convert wchar_t to char in C? Please suggest the code.

    How one can convert wchar_t to char in C? Please suggest the code.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    46
    Quote Originally Posted by forumuser View Post
    How one can convert wchar_t to char in C? Please suggest the code.
    The following page would seem to do what you want.
    Converting wchar_t to char

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    narrow will, however, lose information on the character. So it really depends on what the wstring contains.

    Usually, a normal string contains an ASCII string. If you don't know what ASCII is, look it up.
    A wstring character is 2 bytes in Linux and 4 bytes in Windows. And whatever it contains depends on how you use it. To be honest, I think a wide string actually degrades the portability because of this. Because what are you going to put in it? A UTF16 string? A UTF32 string is not portable.

    If your wide string is in UTF16 or UTF32, however, you can't simply narrow it. You'll need some way of representing special characters. For this, you can use UTF8 - normally.

    I suggest you to read at least the wikipedia entries on unicode and
    Code:
    UTF{8,16,32}*
    . They're quite good. And you'll understand what I mean if you don't already. Basically, I'd use UTF8 in memory. So who needs wide strings?

    *) This is put in a code block because the stupid forum software doesn't allow posting this without it. It thinks it's code. Well, it's not. Stupid software.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Given your question, and given this is C.
    Code:
    wchar_t x = ...;
    char y = (char) x;
    Perhaps ask a smarter question.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by EVOEx View Post
    ...A wstring character is 2 bytes in Linux and 4 bytes in Windows...
    That's not true. A wstring (eg wchar_t) character is 2 bytes under Windows.
    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    That's not true. A wstring (eg wchar_t) character is 2 bytes under Windows.
    And 4 bytes in Linux.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Code:
    void convert_to_string(WCHAR *crap, char *buf) {
    // convert wide-char to normal char
    while (*buf++ = (char)*crap++); }

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <locale.h>
    
    int main()
    {
        // U+0192 : LATIN SMALL LETTER F WITH HOOK
        const wchar_t f_hook = 0x0192;
    
        // this character is 0x85 in CP1252
        setlocale(LC_ALL, ".1252");
        char c;
        wctomb(&c, f_hook);
        printf("%c = 0x%X\n", c, (unsigned char)c);
        return 0;
    }//main
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. convert unsigned char to char *
    By keeper in forum C++ Programming
    Replies: 2
    Last Post: 03-03-2006, 06:53 PM
  4. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM