将wchar_t转换为char [英] Convert wchar_t to char

查看:154
本文介绍了将wchar_t转换为char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这样做是否安全?

  wchar_t wide = / * something * /; 
assert(wide> = 0&&&< 256&&&&
char myChar = static_cast< char>(wide);

如果我确定宽字符将在ASCII范围内。

解决方案

assert 用于确保调试模式下的某些内容为true,发布版本。更好地使用 if 语句,并且对于超出范围的字符使用备用计划,除非通过程序错误获取超出范围的字符的唯一方法。 p>

此外,根据您的字符编码,您可能会发现Unicode字符0x80到0xff和它们的 char 版本之间的差异。


I was wondering is it safe to do so?

wchar_t wide = /* something */;
assert(wide >= 0 && wide < 256 &&);
char myChar = static_cast<char>(wide);

If I am pretty sure the wide char will fall within ASCII range.

解决方案

assert is for ensuring that something is true in a debug mode, without it having any effect in a release build. Better to use an if statement and have an alternate plan for characters that are outside the range, unless the only way to get characters outside the range is through a program bug.

Also, depending on your character encoding, you might find a difference between the Unicode characters 0x80 through 0xff and their char version.

这篇关于将wchar_t转换为char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆