Re: Convert Byte [] to byte
<dhu.prasanna@gmail.com> ha scritto nel messaggio
news:5440539b-d59d-497d-afdb-31baeabe1f41@d27g2000prf.googlegroups.com...
i dont know how to convert byte [] to hexdecimal value
im having BYTE m[3];
by using sprintf i changed into the string its give value 0x12f32c.
then i printed one by one value
If you want to convert an array of BYTEs, you may use a function like so:
<code>
// ---------------------------------------------------------------------
// Converts a vector of bytes into an hex string.
// ---------------------------------------------------------------------
CString ByteVectorToHexString( const std::vector< BYTE > & bytes )
{
if ( bytes.empty() )
return _T("");
// Will contain the complete string
CString result;
// Temporary buffer to convert one byte to hex
TCHAR buf[10];
// For each byte in the array
const int count = static_cast<int>( bytes.size() );
for ( int i = 0; i < count; i++ )
{
// Print the hex byte to temporary string buffer
int b = static_cast<int>( bytes[i] );
StringCbPrintf( buf, sizeof(buf), _T("%02X"), b );
// Concatenate this byte to the rest of string
result += buf;
}
return result;
}
</code>
I store the byte array in a std::vector<BYTE>.
You can use the above function like so:
<code>
std::vector< BYTE > bytes(3);
bytes[0] = 0x2C;
bytes[1] = 0xF3;
bytes[2] = 0x12;
// Print
MessageBox(
NULL,
ByteVectorToHexString(bytes),
_T("Test"),
MB_OK );
</code>
You need to #include <vector> for the std::vector class, and <StrSafe.h> for
the StringCbPrintf function.
then i want to change the m[3] value into the 0x378d02..
how can do this one?? Pls help me.. ITs urgent..
If you want to change the values of the bytes stored into the array, you may
use code like so:
bytes[0] = 0x02;
bytes[1] = 0x8D;
bytes[2] = 0x37;
...
If you want to check array bounds, you can use at() instead of operator[]
(i.e. bytes.at(1) = ... ).
If you want to change vector size, you can use std::resize.
HTH,
Giovanni