Click here to Skip to main content
12,065,045 members (45,436 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VC8.0 C++
How to convert BYTE* to CString? I will be very thankful to you...
Posted 21-Apr-11 2:18am
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
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
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.160204.4 | Last Updated 30 May 2014
Copyright © CodeProject, 1999-2016
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