D
dominolog
Hello
I want to convert a char* buffer to a std::string containing a hex
description of it. I use a std::stringstream in the following manner:
std::tstring ToHex( const char* buffer, size_t size, bool withSize )
{
std::tstringstream str;
str.setf(std::ios_base::hex, std::ios::basefield);
str.setf(std::ios_base::uppercase);
str.fill( _T('0') );
for ( size_t i=0; i<size; ++i )
{
str << std::hex << std::setw(2) << (unsigned short)buffer;
}
return str.str();
}
int main()
{
char buffer[255];
for ( int i=0; i<sizeof buffer; ++i )
{
buffer = i;
}
const std::tstring hex = ToHex( buffer, sizeof buffer, true );
std::cout << hex.c_str() << std::endl;
}
The problem is that for values bigger that 127 (0x7F) the hex elements
are padded with 0xFF (like FF80 FF81 etc). It is even if I cast to
unsigned int or int. I'd like to have values 7E 7F 80 81 ... etc. How
to fix it?
Thanks
dominolog
I want to convert a char* buffer to a std::string containing a hex
description of it. I use a std::stringstream in the following manner:
std::tstring ToHex( const char* buffer, size_t size, bool withSize )
{
std::tstringstream str;
str.setf(std::ios_base::hex, std::ios::basefield);
str.setf(std::ios_base::uppercase);
str.fill( _T('0') );
for ( size_t i=0; i<size; ++i )
{
str << std::hex << std::setw(2) << (unsigned short)buffer;
}
return str.str();
}
int main()
{
char buffer[255];
for ( int i=0; i<sizeof buffer; ++i )
{
buffer = i;
}
const std::tstring hex = ToHex( buffer, sizeof buffer, true );
std::cout << hex.c_str() << std::endl;
}
The problem is that for values bigger that 127 (0x7F) the hex elements
are padded with 0xFF (like FF80 FF81 etc). It is even if I cast to
unsigned int or int. I'd like to have values 7E 7F 80 81 ... etc. How
to fix it?
Thanks
dominolog