Thread: Simple way to convert from a WCHAR to a string?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    100

    Simple way to convert from a WCHAR to a string?

    Hello. What's an easy way to convert from a WCHAR to a string? Thanks!

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If you mean to convert a wchar_t string to a char string using standard C, then:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void) {
        wchar_t wstr[100] = L"hello world";
        char str[50];
        wcstombs(str, wstr, 49);
        printf("%u: %s\n", strlen(str), str);
        return 0;
    }
    If you give more information about why you want to do this, there may be a better way. E.g., you may want to turn off unicode in MSVC++.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-09-2010, 10:58 AM
  2. Zeroed WCHAR
    By gadu in forum C Programming
    Replies: 3
    Last Post: 06-23-2008, 09:41 PM
  3. WChar to char
    By MK4554 in forum C++ Programming
    Replies: 19
    Last Post: 04-20-2006, 12:14 AM
  4. C Datatype WCHAR
    By GUI_XP in forum C Programming
    Replies: 2
    Last Post: 11-29-2002, 06:24 PM
  5. char to WCHAR
    By pinkcheese in forum Windows Programming
    Replies: 5
    Last Post: 07-28-2002, 06:50 PM