Re: convert BYTE array into a CString

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance




"Mirs" <mirs@xxxxxxxxx> ha scritto nel messaggio
news:el4%23j3gWJHA.1328@xxxxxxxxxxxxxxxxxxxxxxx
I need to convert BYTE array ( like BYTE buffer[8] ) into a CString object.
Is it possible and how?

To add to what Dave correctly wrote, if your purpose is instead to build a
string containing the bytes stored in the array, printed in hex, you could
use code like this:

<code>

// Returns a string containing input bytes (stored in array)
// printed in hex.
// Each byte is separated by the next one using a space.
CString FormatByteArrayToString(const BYTE * pb, int count)
{
// Check input parameters
ASSERT(pb != NULL);
ASSERT(count > 0);

// This string will store the bytes in hex
CString result;

// Print 1st byte
result.Format(_T("%02X"), pb[0]);

// For each byte from 2nd to last:
for (int i = 1; i < count; i++)
{
// Format current byte in hex, and separate it
// by previous one using a space
CString hexByte;
hexByte.Format(_T(" %02X"), pb[i]);

// Add current byte final string
result += hexByte;
}

return result;
}


inline CString FormatByteArrayToString(const std::vector<BYTE> & byteArray)
{
return FormatByteArrayToString(&byteArray[0], byteArray.size());
}

</code>

The above functions can be used like this:

<code>

std::vector<BYTE> data;
data.push_back(3);
data.push_back(10);
data.push_back(0);
data.push_back(15);

CString result = FormatByteArrayToString(data);
MessageBox(NULL, result, _T("Bytes in the array:"), MB_OK);

</code>


Giovanni


.



Relevant Pages

  • Hex editor display - can this be more pythonic?
    ... I'm building a hex line editor as a first real Python programming exercise. ... I had considered using the .translatemethod of strings, however this would require a larger translation table than my printable string. ... Where printing chars are shown in parenthesis, characters with Python escape sequences will be shown as their escapes in parens., while non-printing chars with no escapes will be shown with nothing in parens. ...
    (comp.lang.python)
  • Re: compilation problem
    ... want this utility to print decimal for hex numbers entered. ... IF the third argument passed is a string specifying a decimal number ... TERMINATE with an EXIT_SUCCESS returncode ...
    (comp.lang.c)
  • Re: Need a sysRPL CRC16 routine.
    ... Thanks for your thoughts on the string> hex and vice versa. ... Sysrpl is not particularly intuitive on this point. ... effort he has put into the Debug4x programming environment. ...
    (comp.sys.hp48)
  • Entering strings as user input but interpreting as Python input (sort of)
    ... I would want the quoted part to be interpreted as if I entered it into Python itself as: ... In other words, if a quoted string occurs in the user input, I want only that part to be treated as a Python string. ... The point of this in the context of the hex editor is that the user should be able to enter hex bytes without qualifications like "0xXX" but rather as simply: "0A 1B 2C" etc. but also be able to input a string without having to type in hex ASCII codes. ...
    (comp.lang.python)
  • Re: Converting ascii to hex
    ... Why such meaningless and unreadable ... hex constants? ... Did you perhaps mean that "ciptmp is a CString ... For that matter, since you don't show how the string is read in, how do you know that the ...
    (microsoft.public.vc.mfc)