Conversion functions between ascii and utf8 strings

Source: Internet
Author: User

Conversion functions between ASCII and utf8 strings

Author: Lua Date: 2005-8-15 QQ: 382689788

Three functions are published here:

# Pragma warning (Disable: 4172) // return the address of the local or temporary variable
/**
*/Brief converts a multi-byte string into a dual-byte string
*/Param codePage common codePage (cp_acp, cp_utf8)
*/Return wchar_t *: return the temporary variable. The external variable is responsible for analysis,
*/
Const wchar_t * atow (lpctstr STR, uint codePage)
{
Wchar_t wstr [4096];
Memset (wstr, 0, 4096 );
Long llen = multibytetowidechar (codePage, 0, STR,-1, wstr, 4096 );
Wstr [llen] = l'/0 ';
Return wstr;
}
/**
*/Brief converts a dual-byte string into a multi-byte string
*/CodePage (CP_ACP, CP_UTF8)
*/Return char * returns temporary variables. The external component is responsible for structure analysis,
*/
Const char * wtoa (const wchar_t * wstr, UINT codePage)
{
Char szANSIString [4096];
WideCharToMultiByte (codePage, 0, wstr,-1, szANSIString, 4096, NULL, NULL );
Return szANSIString;
}
/**
*/The ascii string provided outside the brief ascii string to utf8 string must have enough length to save the string after utf8 conversion.
*/Param ilen str maximum buffer Length
*/Param str is the string to be converted, and it also needs to store the buffer zone of the conversion target. Therefore, it must be large enough to specify strlen (str) * 3.
*/Return returns the converted length.
*/
# Pragma warning (disable: 4267) // convert from "size_t" to "int", data may be lost
Long atou (LPTSTR str, int ilen)
{
Wchar_t * wstr = new wchar_t [4096];
Memset (wstr, 0, sizeof (wchar_t) * 4096 );
Long lLen = maid (CP_ACP, 0, str,-1, wstr, 4096 );
LLen = WideCharToMultiByte (CP_UTF8, 0, wstr,-1, str, ilen, NULL, NULL );
Delete [] wstr;
Return lLen;
}

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.