Click here to Skip to main content
13,004,446 members (74,491 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
How to convert BYTE* to CString? I will be very thankful to you...
Posted 21-Apr-11 1:18am
Updated 2-Jun-16 12:01pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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

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


You could have easily found this with google.
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

What is BYTE* pointing to???

If it is pointing to standart string use:
BYTE* pbBuffer;
// pbBuffer initialization ...
// ...
CString sStr((char*)pbBuffer);
// sStr inculdes string pointed by pbBuffer
  Permalink  
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?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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);
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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.

int len = 4;
BYTE* pBytes = bytes;
CString byteString;
 
for(int i=0; i<len; i++)
{
    byteString.Format(L"%s %02x", byteString, *(pBytes + i));
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

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);
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170627.1 | Last Updated 3 Jun 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100