Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 28th, 2008, 05:05 PM
dominolog
Guest
 
Posts: n/a
Default stringstream bin buffer to hex string conversion

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
  #2  
Old August 28th, 2008, 05:55 PM
Obnoxious User
Guest
 
Posts: n/a
Default Re: stringstream bin buffer to hex string conversion

On Thu, 28 Aug 2008 08:55:29 -0700, dominolog wrote:
Quote:
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.

--
OU
  #3  
Old August 28th, 2008, 06:15 PM
dominolog
Guest
 
Posts: n/a
Default Re: stringstream bin buffer to hex string conversion

On 28 Sie, 18:15, Obnoxious User <O...@127.0.0.1wrote:
Quote:
On Thu, 28 Aug 2008 08:55:29 -0700, dominolog wrote:
Quote:
Hello
>
Quote:
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:
>
Quote:
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
}
  #4  
Old August 30th, 2008, 05:55 PM
andy_westken@hotmail.com
Guest
 
Posts: n/a
Default Re: stringstream bin buffer to hex string conversion

On 28 Aug, 16:55, dominolog <dominiktomc...@gmail.comwrote:
Quote:
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
  #5  
Old September 2nd, 2008, 02:25 PM
dominolog
Guest
 
Posts: n/a
Default Re: stringstream bin buffer to hex string conversion

On 30 Sie, 18:52, andy_west...@hotmail.com wrote:
Quote:
On 28 Aug, 16:55, dominolog <dominiktomc...@gmail.comwrote:
>
Quote:
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?
>
Quote:
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. !!!!
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles