how can i convert a BYTE value into CString
-
hi to all itook a varible BYTE *bByte and want to convert bByte[1] into a CString type varible,bByte varible has hex value, i done CString csValue=(CHAR)bByte; where am i wrong
-
hi to all itook a varible BYTE *bByte and want to convert bByte[1] into a CString type varible,bByte varible has hex value, i done CString csValue=(CHAR)bByte; where am i wrong
Assuming that you want to create a string representation of the number (that is 32 => "32" rather than 32 => " " – space being the character with the ASCII code 32), you can use something like this:
CString x; BYTE b = 123; x.Format("%u", (unsigned int) b); // "123"
orx.Format("%x", (unsigned int) b); // "7b"
You need to convert it to unsigned int because there doesn't seem to be a way to specify a 1-byte integer in the format string. Or, if you feel adventurous, you can try stringstream[^] or The Boost Format Library[^].Florin Crisan
-
hi to all itook a varible BYTE *bByte and want to convert bByte[1] into a CString type varible,bByte varible has hex value, i done CString csValue=(CHAR)bByte; where am i wrong
You can use CString::Format csValue.Format( "%x", bByte[1] );
- NS - [ODBaseBtn]
modified on Monday, December 31, 2007 9:36:06 AM
-
hi to all itook a varible BYTE *bByte and want to convert bByte[1] into a CString type varible,bByte varible has hex value, i done CString csValue=(CHAR)bByte; where am i wrong
Hi, You are attempting to cast a BYTE* into a CHAR. A BYTE* is a pointer to BYTE data....so the casted CHAR will be a CHAR representation of the pointer address of bByte.
CString csValue = (CHAR)bByte[0]; // for individual character
orCString csValue = (CHAR*)bByte; // for entire byte array // be sure the byte array has a null terminator character '\0' at the end!
If you want to show the hex value of the byte, you can do something like the following:// for numerical value in hex i.e. "0A" if bByte[0] == 0x0A. CString csValue; csValue.Format(_T("%02X"), bByte[0]);
If you wanted to show the entire contents of the byte array as hex then you will need to wrap the above within a loop and concatenate the result each time to the result string.// For example, the byte array { 0x0A, 0x0B, 0x12 } // will would create the string "0A0B12". CString csValue, csTemp; BYTE *pActiveByte = bByte; while(*pActiveByte) { csTemp.Format(_T("%02X"), *pActiveByte++); csValue.Append(csTemp); }
I hope that this helps... Lea Hayes -
Assuming that you want to create a string representation of the number (that is 32 => "32" rather than 32 => " " – space being the character with the ASCII code 32), you can use something like this:
CString x; BYTE b = 123; x.Format("%u", (unsigned int) b); // "123"
orx.Format("%x", (unsigned int) b); // "7b"
You need to convert it to unsigned int because there doesn't seem to be a way to specify a 1-byte integer in the format string. Or, if you feel adventurous, you can try stringstream[^] or The Boost Format Library[^].Florin Crisan
In your code,
bByte
is actually a pointer to aBYTE
rather than aBYTE
. You should have named it better:pbySomethingUseful
.p
stands for pointer,by
stands forbyte
(b
stands forbool
, and that one is actually as big as anint
). The rest should be a useful name :) So your code should look like:csValue.Format("%x", (unsigned int) (*pbySomethingUseful))
.Florin Crisan
-
Hi, You are attempting to cast a BYTE* into a CHAR. A BYTE* is a pointer to BYTE data....so the casted CHAR will be a CHAR representation of the pointer address of bByte.
CString csValue = (CHAR)bByte[0]; // for individual character
orCString csValue = (CHAR*)bByte; // for entire byte array // be sure the byte array has a null terminator character '\0' at the end!
If you want to show the hex value of the byte, you can do something like the following:// for numerical value in hex i.e. "0A" if bByte[0] == 0x0A. CString csValue; csValue.Format(_T("%02X"), bByte[0]);
If you wanted to show the entire contents of the byte array as hex then you will need to wrap the above within a loop and concatenate the result each time to the result string.// For example, the byte array { 0x0A, 0x0B, 0x12 } // will would create the string "0A0B12". CString csValue, csTemp; BYTE *pActiveByte = bByte; while(*pActiveByte) { csTemp.Format(_T("%02X"), *pActiveByte++); csValue.Append(csTemp); }
I hope that this helps... Lea HayesI must say that I am little surprised that it works with a
BYTE
. Weren't%x
and%u
supposed to take a (four-byte) integer?Florin Crisan