Convert wstring to char *

Source: Internet
Author: User

# Include <iostream>
# Include <fstream>
# Include <windows. h>
Using namespace STD;

Int main ()
{
File * FP;
Wchar [5] = l "equal to equal"; // defines a wide-Byte variable, initially equal to "equal"
Fp = fopen ("1.txt"," W + "); // open the file called Odo
Fwprintf (FP, l "% s \ n", wchar); // output to file
Fclose (FP); // close the file
Wchar wc2 [5]; // defines the second wide BYTE Variable

// There are many width-byte operations starting with WC. Corresponds to Str.
Wcscpy (wc2, wchar); // copy.
Int n = wcscmp (wc2, wchar); // comparison
If (n = 0)
{
Wprintf (L "equal \ n"); // do you notice that there is no problem with wprintf.
}

Char STR [10]; // defines the char character.
N = wcstombs (STR, wc2, 9); // converts a wide byte to muiltychar
Printf ("% s \ n", STR); // output result

For (INT I = 0; I <5; ++ I)
{
Wc2 [I] = L '1' + I;
}
Wc2 [4] = 0;

N = wcstombs (STR, wc2, 9); // converts a wide byte to muiltychar
Printf ("% s \ n", STR); // output result
// Another Conversion Method
N = widechartomultibyte (cp_acp, null, wchar, wcslen (wchar) + 1, STR, 10, 0, 0 );
Printf ("% s \ n", STR );
Char str2 [10] = "1 ";
Wchar wc3 [10];
N = multibytetowidechar (cp_acp, null, str2, strlen (str2) + 1, wc3, 10); // Char to the width byte.

System ("pause ");
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.