首页 > 代码库 > [VC]char 和 wchar_t相互转化
[VC]char 和 wchar_t相互转化
- #include <windows.h>
- #include <stdio.h>
- //function: charTowchar
- //purpose:char to WCHAR 、wchar_t、LPWSTR etc
- void charTowchar(const char *chr, wchar_t *wchar, int size)
- {
- MultiByteToWideChar( CP_ACP, 0, chr,
- strlen(chr)+1, wchar, size/sizeof(wchar[0]) );
- }
- //function: wcharTochar
- //purpose:WCHAR 、wchar_t、LPWSTR to char
- void wcharTochar(const wchar_t *wchar, char *chr, int length)
- {
- WideCharToMultiByte( CP_ACP, 0, wchar, -1,
- chr, length, NULL, NULL );
- }
- int main (void)
- {
- char chr[128];
- wchar_t *wchar = L"陈鸿钦";
- //wchar_t to char
- wcharTochar(wchar, chr, sizeof(chr));
- printf("char is %s\n", chr);
- //char to wchar_t
- wchar = (wchar_t *)malloc(sizeof(wchar_t) * 64);
- charTowchar(chr, wchar, sizeof(wchar_t) * 64);
- wprintf_s(L"%s\n", wchar);//
- getchar();
- return 0;
- }
- Requirements: Ws2_32.lib
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。