Conversion between wchar [] and char []

Source: Internet
Author: User

I searched the internet for nearly an hour and did not find the appropriate transfer type code. I wrote it in 10 minutes, and I was a little unconfident. Lazy !! Let's post it and share it with you to save your time.

Extern "C"
{

Void _ declspec (dllexport) W2C (wchar * pwstr, char * CSTR)
{
Char * pwssrc = (char *) pwstr;

Ulong length = 0;

While (1)
{
If (* pwssrc! = 0x00 & * (pwssrc + 1) = 0x00)
{
CSTR [length ++] = * pwssrc;

Pwssrc + = 2;

} Else
{
CSTR [length] = 0x00;

Break;
}
}

}

Void _ declspec (dllexport) c2w (char * CSTR, wchar * wstr)
{
 
Char * pcstr = CSTR;

Wchar * pwcstr = wstr;

Ulong I = 0;
Ulong J = 0;

While (1)
{
If (pcstr [I]! = 0x00)
{
(Char *) pwcstr) [J] = pcstr [I];
(Char *) pwcstr) [++ J] = 0x00;

I ++;
J ++;

} Else
{
(Char *) pwcstr) [J] = 0x00;
(Char *) pwcstr) [++ J] = 0x00;

Break;
}
}
 
}

}

The preceding method has limitations when used recently. The strings cannot contain Chinese characters.

The improved version is as follows:

Namespace charobj
{
//--------------------------------
// Convert multiple character sets to Unicode wide character sets
//--------------------------------
Void mutiltowide (char szmutilsrcdata [], DWORD dwmutilsrcdatabufsize,
Wchar lpwidecharstr [], DWORD dwwidecharbufsize)
{

Multibytetowidechar (cp_acp, 0,
Szmutilsrcdata,
Dwmutilsrcdatabufsize,
Lpwidecharstr,
Dwwidecharbufsize );
}

//--------------------------------
// Convert Unicode wide character set to multi-Character Set
//--------------------------------
Void widetomutil (wchar lpwidecharstr [], DWORD dwwidecharbufsize,
Char szmutilsrcdata [], DWORD dwmutilsrcdatabufsize)
{

Widechartomultibyte (cp_acp, // code page
0, // performance and mapping flags
Lpwidecharstr, // wide-character string
Dwwidecharbufsize, // Number of chars in string
Szmutilsrcdata, // buffer for New String
Dwmutilsrcdatabufsize,
Null,
Null );
}

 

}

 

 

 

C Runtime Library Standard Functions

Mbstowcs

Wcstombs

 

 

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.