C/C++笔记之char *与wchar_t *的相互转换
char *和wchar_t *的相互转换,可使用标准库函数
size_t mbstowcs(wchar_t *wcstr, const char *mbstr, size_t count)和size_t wcstombs(char *mbstr, const wchar_t *wcstr, size_t count)
注意
微软文档对mbstowcs的描述有误,mbstowcs, _mbstowcs_l | Microsoft Docs

若mbstowcs成功地转换了源字符串,它返回已转换的多字节字符的数量。这句话是错误的,应参考cppreference的描述,std::mbstowcs - cppreference.com

成功,则返回写入目标数组的宽字符的数量,不包括L'\0'。这句才是正解
另外,根据微软文档的描述,若参数wcstr为NULL,函数返回目标字符串的要求大小(以宽字符为单位)
多字节字符串到宽字符串的转换示例
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "");
const char *mbString = "多字节字符串到宽字符串的转换";
size_t requiredSize = mbstowcs(nullptr, mbString, );
wchar_t *wcString = new wchar_t[requiredSize + ];
if (static_cast<size_t>(-) == mbstowcs(wcString, mbString, requiredSize + ))
{
cout << "Couldn't convert string: invalid multibyte character." << endl;
}
else
{
wcout << "wcString: " << wcString << endl;
}
delete[] wcString;
return ;
}
宽字符串到多字节字符串的转换示例
#include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "");
const wchar_t *wcString = L"宽字符串到多字节字符串的转换";
size_t requiredSize = wcstombs(nullptr, wcString, );
char *mbString = new char[requiredSize + ];
if (static_cast<size_t>(-) == wcstombs(mbString, wcString, requiredSize + ))
{
cout << "Couldn't convert string: invalid wide character" << endl;
}
else
{
cout << mbString << endl;
}
delete[] mbString;
return ;
}
C/C++笔记之char *与wchar_t *的相互转换的更多相关文章
- 深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换 [转]
本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下. #ifndef USE_H_ #define USE_H_ # ...
- 深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换
本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下-复制代码 代码如下: #ifndef USE_H_ ...
- char* 和 wchar_t* 如何互相转换
char* 和 wchar_t* 如何互相转换 C函数可以用 wcstombs - 将宽字符转换成多字符 WCHAR -> CHAR mbstowcs - 把多字符把转换成宽字符 C ...
- C++中char*与wchar_t*之间的转换
http://blog.163.com/tianshi_17th/blog/static/4856418920085209414977/ 关于C++中的char*与wchar_t*这两种类型的相互转换 ...
- char 转wchar_t 及wchar_t转char
利用WideCharToMultiByte函数来转换,该函数映射一个unicode字符串到一个多字节字符串.通常适合于window平台上使用. #include <tchar.h> #in ...
- 【转】char*,const char*和string的相互转换
1. string转const char* string s = "abc"; const char* c_s = s.c_str(); 2. const char*转string ...
- 自己写实现char TO wchar_t 的转换
wchar_t CharToWChart(char nChar){ wchar_t nR; nR=nChar+32*256; return nR;}//--------------- ...
- char,wchar_t 长度
(测试环境:VC++6.0) char类型 wchar_t类型 类型大小(32位系统) 8位 16位 常量字符表示法 'A' L'A' 或 'A' 常量字符串表示法 'hello' L'hello' ...
- (c++) int 转 string,char*,const char*和string的相互转换
一.int 和string的相互转换 1 int 转化为 string c++ //char *itoa( int value, char *string,int radix); // 原型说明: / ...
随机推荐
- Beyond compare vs kdiff3
這裡使用的 kdiff3 版本是 0.9.98 基於以下 三點,最終選擇了 beyond compare 1. kdiff3 不能刪檔案, 以下為例,不能刪1 2. kdiff3 ...
- 捕获错误并发邮件 register_shutdown_function
/** * 脚本程序异常捕获 */ function handleError() { global $config; $error = error_get_last(); if (isset($err ...
- 大视野 1016: [JSOI2008]最小生成树计数(最小生成树)
总结:此类题需要耐心观察规律,大胆猜想,然后证明猜想,得到有用的性质,然后解答. 简单的说:找隐含性质. 传送门:http://61.187.179.132/JudgeOnline/problem.p ...
- 使用 IntelliJ IDEA 开发一般 Java 应用程序时配置 Allatori 进行代码混淆
使用 IntelliJ IDEA 开发一般 Java 应用程序时通过 Allatori 进行代码混淆非常容易配置,下面总结一下本人经验,介绍一下配置方法. 首先在 IDEA 的 Module 所在硬盘 ...
- IntelliJ IDEA出现:java: Compilation failed: internal java compiler error的问题解决
这两处地方要同时修改成一样的. 参考: http://blog.csdn.net/u011275152/article/details/45242201
- 自签名证书安全性问题研究https(ssl)
先看下https(ssl)的好处,以及为什么要用: http://imweb.io/topic/565c71673ad940357eb99879 https://zh.wikipedia.org/wi ...
- 在C#的数据类型中,什么属于值类型,什么属于引用类型
转自原文 在C#的数据类型中,什么属于值类型,什么属于引用类型 类型:整数,浮点数,高精度浮点数,布尔,字符,结构,枚举引用类型:对象(Object),字符串,类,接口,委托,数组除了值类型和引用类型 ...
- Class definition
Prerequisite Articles (None) Related Articles Category Accessor method A class definition is the spe ...
- CodeSign error: code signing is required for product type 'Application' in SDK 'iOS 7.0'
这个一般是证书设置的问题, 在build settings中找到 Code Signing->Code Signing Identity修改成有效的证书即可
- Android 实现Activity后台运行
有时需要让activity在后台运行,具体实现方法如下: 在AndroidManifest.xml中,activity属性中增加: android:theme="@style/Backgro ...