C++

    xiaoxiao2021-03-28  31

    #include <iostream> #include <windows.h> #include <locale.h> using namespace std;

    int main(void) { char multichar[]={"窄字符"}; wchar_t widechar[]={L"宽字符"};

    DWORD sizeMByte = WideCharToMultiByte(CP_OEMCP, 0, widechar, -1, NULL, 0, NULL, FALSE); char* newChar = new char[sizeMByte]; WideCharToMultiByte(CP_OEMCP, 0, widechar, -1, newChar, sizeMByte, NULL, FALSE); cout << "原始为窄字符: " << multichar << endl; cout << "宽字符转换为窄字符:   " << newChar << endl; wcout.imbue(locale("chs")); wcout << L"原始为宽字符:   " << widechar << endl; DWORD sizeWChar = MultiByteToWideChar(CP_OEMCP, 0, multichar, -1, NULL, 0); wchar_t* newWChar = new wchar_t[sizeWChar]; MultiByteToWideChar(CP_OEMCP, 0, multichar, -1, newWChar, sizeWChar); wcout << L"窄字符转换为宽字符:   " << newWChar << endl; return 0; }

    将宽字符串wcstr转换为ANSI字符串mbstr   size_t wcstombs( char *mbstr, const wchar_t *wcstr, size_t count );   mbstr   多字节字符的地址   wcstr   宽字符的地址   count   可以存储在多字节字符的最大字节数   将ANSI字符串mbstr转化为宽字符串wcstr   size_t mbstowcs( wchar_t *wcstr, const char *mbstr, size_t count );   Parameters   wcstr   宽字符串的地址   mbstr   多字节字符串(ANSI)的地址   count   要转换的多字节的字符的个数   Example   /* MBSTOWCS.CPP illustrates the behavior of the mbstowcs function   */   #include <stdlib.h>   #include <stdio.h>   void main( void )   {   int i;   char *pmbnull = NULL;   char *pmbhello = (char *)malloc( MB_CUR_MAX );   wchar_t *pwchello = L"Hi";   wchar_t *pwc = (wchar_t *)malloc( sizeof( wchar_t ));   printf( "Convert to multibyte string:\n" );   i = wcstombs( pmbhello, pwchello, MB_CUR_MAX );   printf( "\tCharacters converted: %u\n", i );   printf( "\tHex value of first" );   printf( " multibyte character: %#.4x\n\n", pmbhello );   printf( "Convert back to wide-character string:\n" );   i = mbstowcs( pwc, pmbhello, MB_CUR_MAX );   printf( "\tCharacters converted: %u\n", i );   printf( "\tHex value of first" );   printf( " wide character: %#.4x\n\n", pwc );   delete[] pmbhello;   delete[] pwc ;   //该例子示例摘自msdn,我觉得这里有内存泄漏,所以我加入了   //最后两行,应为这里涉及到动态内存分配,   //ms-help://MS.MSDNQTR.2003FEB.2052/wcecrt/htm/_wcecrt_mbstowcs.htm   }   Output   Convert to multibyte string:   Characters converted: 1   Hex value of first multibyte character: 0x0e1a   Convert back to wide-character string:

    转载请注明原文地址: https://ju.6miu.com/read-664638.html

    最新回复(0)