从'const WCHAR'到'BYTE'的反转,可能会丢失数据 [英] onversion from 'const WCHAR' to 'BYTE', possible loss of data

查看:92
本文介绍了从'const WCHAR'到'BYTE'的反转,可能会丢失数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

警告C4244:'初始化':从'const WCHAR'转换为'BYTE',可能会丢失数据



如何解决这个问题..请让我知道

warning C4244: 'initializing' : conversion from 'const WCHAR' to 'BYTE', possible loss of data

how t osolve this problem .. please let me know

推荐答案

WCHAR是2个字节长,你想把它分配给一个字节
WCHAR is 2 bytes long and you want to assign it to one byte


嗯, WCHAR数据类型包含16位Unicode字符 [ ^ ]和BYTE是一个8位 unsigned char 。换句话说,WCHAR的大小是BYTE的两倍。如果在WCHAR的上半部分有任何相关数据,它将会丢失,因为只有较低的分配给BYTE。



如果你确定上半部分没有你关心的数据,你可以通过使用显式强制转换避免警告,告诉编译器你知道你在做什么:
Well, the WCHAR data type contains a 16-bit Unicode character[^] and a BYTE is an 8-bit unsigned char. In other words, WCHAR is twice the size of BYTE. If there is any relevant data in the upper half of the WCHAR, it will be lost, since only the lower is assigned to the BYTE.

If you are sure the upper half has no data you care about, you can avoid the warning by using an explicit cast, which tells the compiler you know what you are doing:
WCHAR ch = 0x0132;               /* 16 bits of data */
BYTE b1 = ch;                    /* Warning: possible loss of data. b1 is 0x32 */
BYTE b2 = (BYTE)ch;              /* C cast, no warning. b2 is 0x32 */
BYTE b3 = static_cast<byte>(ch); /* C++ cast, no warning. b3 is 0x32 */


这篇关于从'const WCHAR'到'BYTE'的反转,可能会丢失数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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