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;
}