Simple way to convert from a WCHAR to a string?

This is a discussion on Simple way to convert from a WCHAR to a string? within the C++ Programming forums, part of the General Programming Boards category; Hello. What's an easy way to convert from a WCHAR to a string? Thanks!...

  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, 11:58 AM
  2. Zeroed WCHAR
    By gadu in forum C Programming
    Replies: 3
    Last Post: 06-23-2008, 10:41 PM
  3. WChar to char
    By MK4554 in forum C++ Programming
    Replies: 19
    Last Post: 04-20-2006, 01: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, 07:50 PM

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