byte * to CString

Hi guys,
I have a function myfunc(byte * pData, unsigned long datalength)
how can I put pData content into a CString ?
best regards
[158 byte] By [jna96864] at [2007-11-20 1:37:31]
# 1 Re: byte * to CString
Why do you need to put a BYTE* into a CString?
Siddhartha at 2007-11-10 23:17:52 >
# 2 Re: byte * to CString
All that you need it's sscanf() function.
Some references you can find here:
http://www.dev-archive.com/forum/showthread.php?t=231056
http://www.dev-archive.com/forum/showthread.php?t=231054
I think that's all that you need.
Enjoy! ;)

Why do you need to put a BYTE* into a CString?
Nice question, Siddhartha! :wave:
Maximus_X at 2007-11-10 23:18:52 >
# 3 Re: byte * to CString
All that you need it's sscanf() function.
Hm, I guess you mean sprintf(). Different ball games...

Anyway, you can simply use the Format() method of CString.

void myfunc(byte * pData, unsigned long datalength)
{
CString text;
for(unsigned int i = 0; i < datalength; i++)
{
CString temp;
temp.Format("%d", data[i]); // or "%.2X" if you want hex
text += temp; if(i + 1 != datalength) text += ", ";
}
}

Something like that...
cilu at 2007-11-10 23:19:57 >
# 4 Re: byte * to CString
If you are not doing a UNICODE build thus TCHAR = char then you can do the following:

byte b[] = { 'a', 'b', 'c'};
CString str( (LPCTSTR) b, 3);

In your case it would be:

CString myfunc(byte * pData, unsigned long datalength)
{
return CString( (LPCTSTR) pData, datalength);
}
PadexArt at 2007-11-10 23:20:51 >
# 5 Re: byte * to CString
Only if byte * pData is atually a string and not some various data. ;)
cilu at 2007-11-10 23:21:55 >
# 6 Re: byte * to CString
Only if byte * pData is atually a string and not some various data. ;)

ofc :)
PadexArt at 2007-11-10 23:22:54 >
# 7 Re: byte * to CString
Thank you guys, I use this

void myfunc(byte * pData, unsigned long datalength)
{
CString text;
for(unsigned int i = 0; i < datalength; i++)
{
CString temp;
temp.Format("%d", data[i]); // or "%.2X" if you want hex
text += temp; if(i + 1 != datalength) text += ", ";
}
}
jna96864 at 2007-11-10 23:23:58 >
# 8 Re: byte * to CString
This code is correct but it is painfully slow. If you invoke it rarelly than it is almost ok but I would try to find a faster ( than VB) solution. :D

PS: although at this time of the day and ahead of a big footbal game I cannot think of anything faster. ;)
PadexArt at 2007-11-10 23:24:57 >
# 9 Re: byte * to CString
Thank you guys, I use this...
The (laughed at) question "why?" is still not answered...
What's a "byte"? And what's in pData?
VladimirF at 2007-11-10 23:25:54 >
# 10 Re: byte * to CString
The (laughed at) question "why?" is still not answered...
What's a "byte"?
8 bits?
And what's in pData?
A pointer (possibly invalid) to 8 bits.

:D :D :D :D

Sorry, I just couldn't resist!!!

Viggy
MrViggy at 2007-11-10 23:27:00 >
# 11 Re: byte * to CString
Sorry, I just couldn't resist!!!

I understand. :)
It's just that my compiler (in Visual Studio 8.0) doesn't know the type "byte"...
VladimirF at 2007-11-10 23:27:59 >
# 12 Re: byte * to CString
It should be in 'windef.h':
typedef unsigned char BYTE;
Viggy
MrViggy at 2007-11-10 23:28:57 >
# 13 Re: byte * to CString
It should be in 'windef.h':
typedef unsigned char BYTE;
Viggy
I do know "BYTE", the "byte" I have a problem with...
VladimirF at 2007-11-10 23:29:59 >
# 14 Re: byte * to CString
D'OH, I missed that (it was Siddhartha's response that threw me).

;)

Vig.
MrViggy at 2007-11-10 23:30:59 >
# 15 Re: byte * to CString
I do know "BYTE", the "byte" I have a problem with...It isn't any different...
typedef unsigned char byte;
Though, in the case of the OP - "byte" might also just be a typo for what is a BYTE.
Siddhartha at 2007-11-10 23:32:03 >
# 16 Re: byte * to CString
I do know "BYTE", the "byte" I have a problem with...
It's enought to include <windows.h> to have your compiler recognize byte.
cilu at 2007-11-10 23:33:07 >
# 17 Re: byte * to CString
Why do you need to put a BYTE* into a CString?
Here is a practical example of "putting BYTE* into a CString":
// NOTE: Just example. No checking is made.
HKEY hKey = NULL;
::RegOpenKey(HKEY_CURRENT_USER, _T("Software\\Baba Safta & Co.\\App\\Font"), &hKey);

DWORD dwType = 0;
DWORD cbData = 0;
BYTE* lpData = NULL;
::RegQueryValueEx(hKey, _T("FaceName"), 0, &dwType, lpData, &cbData);
lpData = new BYTE[cbData];
::RegQueryValueEx(hKey, _T("FaceName"), 0, &dwType, lpData, &cbData);
CString strFaceName = (LPCTSTR)lpData;
delete[] lpData;
::RegCloseKey(hKey);
ovidiucucu at 2007-11-10 23:34:10 >
# 18 Re: byte * to CString
Here is a practical example of "putting BYTE* into a CString":I am not looking for random examples.

My question was posed to the OP, and I wished to know the need for this conversion as not every BYTE* points to a NULL terminated string.
lpData = new BYTE[cbData];
::RegQueryValueEx(hKey, _T("FaceName"), 0, &dwType, lpData, &cbData);
CString strFaceName = (LPCTSTR)lpData; Note that you are actually assigning a TCHAR* (which is pointed to by a BYTE*) into a CString - nothing else (lets ignore the missing terminating NULL character in the buffer - relying excessively on API behaviour).

A different way of coding the same would be -
TCHAR* lpData = new TCHAR [cbData / sizeof (TCHAR) + 1];
memset (lpData, 0x00, cbData + sizeof (TCHAR));

::RegQueryValueEx(hKey, _T("FaceName"), 0, &dwType, (LPBYTE)lpData, &cbData);
CString strFaceName (lpData);

delete [] lpData;There isn't anything practical about BYTE* to CString conversion.
Siddhartha at 2007-11-10 23:35:04 >
# 19 Re: byte * to CString
I should imagine how happy can you be when ignore my NOTEs to point "something wrong" in my example(s).
In fact, you have made the same thing in a little bit harder to read form, except an issue which you probably have read in "Writing Secure Code - second edition".
Well, we can continue that way, if you wish: you also didn't check the cbData value gotten after the first call. What about if a wise guy has put in that registry value something huge?...
There isn't anything practical about BYTE* to CString conversion.
I think, you need a "random practical example" walking on the water. :D ;)
ovidiucucu at 2007-11-10 23:36:07 >
# 20 Re: byte * to CString
I should imagine how happy can you be when ignore my NOTEs to point "something wrong" in my example(s).It wasn't just something wrong - assigning a non-NULL terminated string to a CString will certainly cause a memory overrun. This is not trivial.
In fact, you have made the same thing in a little bit harder to read ...My point is exactly that - it is the same thing in two different ways - both are TCHAR* and not BYTE* - as both contain NULL terminated strings.
Well, we can continue that way, if you wish: you also didn't check the cbData value gotten after the first call. What about if a wise guy has put in that registry value something huge?...What is huge? If a value I need is in the registry, and is dynamically allocated for - where is the question of huge or small?

Bottomline was just that there are few practical reasons why a BYTE* passed to a function should be asssigned to a CString, and my question was posed to the OP.
Siddhartha at 2007-11-10 23:37:09 >
# 21 Re: byte * to CString
Note that you are actually assigning a TCHAR* (which is pointed to by a BYTE*) into a CString - nothing else
Thank you teacher! I have noted, something I never knew before. :rolleyes:
For the rest, no more comments, unless opening a neverending discussion. :sick:

PS. Just (you also) Note: I didn't "assign" anything, but called one of CString's constructors. ;)
ovidiucucu at 2007-11-10 23:38:11 >
# 22 Re: byte * to CString
Returning to the other issue discussed here:
What are 'BYTE' and 'byte' identifiers we are talking about? As already pointed before, they are aliases (typedef(s) declared in WINDEF.H and RPCNDR.H, respectively) of fundamental type 'unsigned char', which is one of types guaranteed by standard having 1 byte length:
ISO C++
The sizeof operator yields the number of bytes in the object representation of its operand.
[...]
sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1; the result of sizeof applied to any other fundamental type is implementation-defined.
And what is a byte?
ISO C++
The fundamental storage unit in the C++ memory model is the byte. A byte is at least large enough to contain
any member of the basic execution character set and is composed of a contiguous sequence of bits, the
number of which is implementation-defined.
/i think it's pretty clear, although I'm afraid, my friend Sid has something to add... :D ;)
ovidiucucu at 2007-11-10 23:39:15 >
# 23 Re: byte * to CString
/i think it's pretty clear, although I'm afraid, my friend Sid has something to add...
LOL :D 2xLOL :D 3xLOL :D
cilu at 2007-11-10 23:40:15 >
# 24 Re: byte * to CString
LOL :D 2xLOL :D 3xLOL :D
I would prefer 0xE0F which is syntactically correct... :D

OK, let's return on topic.
ovidiucucu at 2007-11-10 23:41:10 >