Click here to Skip to main content
15,881,877 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How to convert BYTE* to CString? I will be very thankful to you...
Posted
Updated 2-Jun-16 12:01pm

My C++ foo is old and long unused, but this might work:

C++
BYTE* buffer;
CString sStr((char*)buffer);
// or for unicode:
CString str((const wchar_t*)buffer);


You could have easily found this with google.
 
Share this answer
 
Comments
shiwanijaiswal 7-Oct-15 7:49am    
Thank u so much...works great
Mohibur Rashid 2-Jun-16 18:56pm    
I am amused.
Afzaal Ahmad Zeeshan 3-Jun-16 3:45am    
5ed.
What is BYTE* pointing to???

If it is pointing to standart string use:
C++
BYTE* pbBuffer;
// pbBuffer initialization ...
// ...
CString sStr((char*)pbBuffer);
// sStr inculdes string pointed by pbBuffer
 
Share this answer
 
v2
Comments
maddog_ 31-May-16 17:30pm    
I desire to go in opposite direction.. from CString -> const BYTE*
Tried the following line which produces gibberish.

const BYTE* lpb = reinterpret_cast<const byte*>(&csValue);

The purpose of this is I would like to make my function call simple by
passing a CString arguement. Because the the function I'm calling requires
a const BYTE* (for some unknown reason), BYTE is defined as unsigned char
anyway. The function I'm calling is writing a value to the registry.
Any simple suggestions?
What do you need to do?
If pointer should be in the string like 0x0000ffff then use function Format.
CString local_string;
BYTE *local_pointer = (BYTE *)&local_string;
local_string.Format("%x",local_pointer);
 
Share this answer
 
Others have shown you how to convert it to a string assuming the byte* is a pointer to a character array. Here's an alternate answer where I assume that you want a string of the actual bytes the pointer is pointing to.

C++
int len = 4;
BYTE* pBytes = bytes;
CString byteString;

for(int i=0; i<len; i++)
{
    byteString.Format(L"%s %02x", byteString, *(pBytes + i));
}
 
Share this answer
 
BYTE x[5]; x[0] = 'A'; x[1] = 'B'; x[2] = 'C'; x[3] = 'D'; x[4] = '0';

CString str = TEXT("");
CString s;
for(int i=0;i<5;i++)
{
str.Format(L"%C",x[i]);
s += str;
}

AfxMessageBox(s);
 
Share this answer
 
Comments
CHill60 29-May-14 10:41am    
You are 3 years late with this unformatted response

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