Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
warning C4244: 'initializing' : conversion from 'const WCHAR' to 'BYTE', possible loss of data

how t osolve this problem .. please let me know
Posted
Comments
[no name] 21-Apr-15 6:18am    
instead of BYTE can i use widechar

WCHAR is 2 bytes long and you want to assign it to one byte
 
Share this answer
 
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:
C++
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 */
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900