char*怎么转w_char*
时间: 2023-12-22 20:03:23 浏览: 138
以下是两种将char*转换为w_char*的方法:
1.使用mbstowcs_s和wcstombs_s函数进行转换:
```c
#include <stdlib.h>
#include <wchar.h>
char* wchar2char(const wchar_t* wStr){
char *str;
size_t len = wcslen(wStr) + 1;
size_t converted = 0;
str=(char*)malloc(len*sizeof(char));
wcstombs_s(&converted, str, len, wStr, _TRUNCATE);
char* res = str;
free(str);
return res;
}
```
2.使用mbstowcs函数进行转换:
```c
#include <stdlib.h>
#include <wchar.h>
int char2wchar(wchar_t* &wStr,const char* str){
size_t w_len = mbstowcs(NULL, str, 0) + 1;
if (w_len == 0){
wStr = NULL;
return -1;
}
wStr=(wchar_t*)malloc(w_len*sizeof(wchar_t));
if (!wStr){
return -1;
}
mbstowcs(wStr, str, w_len);
return w_len;
}
```
阅读全文