Discussion:
stringstream bin buffer to hex string conversion
(too old to reply)
dominolog
2008-08-28 15:55:29 UTC
Permalink
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[i];
}
return str.str();
}

int main()
{
char buffer[255];
for ( int i=0; i<sizeof buffer; ++i )
{
buffer[i] = 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
Obnoxious User
2008-08-28 16:15:57 UTC
Permalink
Post by dominolog
Hello
I want to convert a char* buffer to a std::string containing a hex
std::tstring ToHex( const char* buffer, size_t size, bool withSize ) {
std::tstringstream str;
What are std::tstring and std::tstringstream? Last time
I checked the std namespace, there were no such things.
--
OU
dominolog
2008-08-28 17:06:23 UTC
Permalink
Post by Obnoxious User
Post by dominolog
Hello
I want to convert a char* buffer to a std::string containing a hex
std::tstring ToHex( const char* buffer, size_t size, bool withSize ) {
    std::tstringstream str;
What are std::tstring and std::tstringstream? Last time
I checked the std namespace, there were no such things.
--
OU
Oh, sorry. It is my "extension". They are simply

namespace std
{
#ifdef _UNICODE
typedef wstring tstring;
typedef wstringstream tstringstream;
#else
typedef string tstring;
typedef stringstream tstringstream;
#endif
}
a***@hotmail.com
2008-08-30 16:52:20 UTC
Permalink
Post by dominolog
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
Remove the sign from your char before casting it to a short

for ( size_t i=0; i<size; ++i )
{
str << std::hex << std::setw(2) << (unsigned short)(unsigned
char)buffer[i];
}
return str.str();

Or store you hex bytes in an unsigned char array to start with.

(And you can - of course - remove the << std::hex as you've already
set hex-mode using setf)

Andy
dominolog
2008-09-02 13:24:57 UTC
Permalink
Post by a***@hotmail.com
Post by dominolog
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
Remove the sign from your char before casting it to a short
    for ( size_t i=0; i<size; ++i )
    {
        str << std::hex << std::setw(2) << (unsigned short)(unsigned
char)buffer[i];
    }
    return str.str();
Or store you hex bytes in an unsigned char array to start with.
(And you can - of course - remove the << std::hex as you've already
set hex-mode using setf)
Andy
Thanks. It works. !!!!

Loading...