stringstream bin buffer to hex string conversion

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
 
O

Obnoxious User

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;

What are std::tstring and std::tstringstream? Last time
I checked the std namespace, there were no such things.
 
D

dominolog

What are std::tstring and std::tstringstream? Last time
I checked the std namespace, there were no such things.

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

andy_westken

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;
}
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
 
D

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;
    }
    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. !!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top