C++
来源:互联网 发布:游戏测试面试题算法 编辑:程序博客网 时间:2024/06/05 09:05
#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:
- c
- c
- c
- c
- C
- c
- c
- c
- C+
- c
- C
- c
- c
- c
- C
- C
- c
- C
- 监控
- WAMP安装配置过程笔记
- Spring参数注入的几种方法
- FaceNet: A Unified Embedding for Face Recognition and Clustering
- 文章标题
- C++
- 插件式换肤框架搭建
- eclipse两种方式解决添加未实现的方法(Add unimplemented methods)
- 深入理解JVM 一GC(下) G1 Garbage Collector
- 系统调用、POSIX、C库、系统命令和内核函数
- Android Bitmap知识梳理学习
- Linux文件操作
- golang九九乘法表,哈哈。
- 页面中有相同id,DOM对节点的获取