473,405 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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
Aug 28 '08 #1
4 7630
On Thu, 28 Aug 2008 08:55:29 -0700, dominolog wrote:
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
Aug 28 '08 #2
On 28 Sie, 18:15, Obnoxious User <O...@127.0.0.1wrote:
On Thu, 28 Aug 2008 08:55:29 -0700, dominolog wrote:
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
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
}
Aug 28 '08 #3
On 28 Aug, 16:55, dominolog <dominiktomc...@gmail.comwrote:
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
Aug 30 '08 #4
On 30 Sie, 18:52, andy_west...@hotmail.com wrote:
On 28 Aug, 16:55, dominolog <dominiktomc...@gmail.comwrote:
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. !!!!
Sep 2 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ellarco | last post by:
Im sorry for asking a question that is surely in the archives somewhere, but I have been unable to locate it. Its about string memory management. I need to dynamically construct a C-style string...
2
by: Woodster | last post by:
I am using std::stringstream to format a string. How can I clear the stringstream variable I am using to "re use" the same variable? Eg: Using std::string std::string buffer; buffer =...
5
by: William Payne | last post by:
How do you get rid the contents in a std::stringstream? I'm using one in a for loop to format some status messages which I print to the screen. The stringstream member function clear() only clears...
5
by: Marcin Kalicinski | last post by:
Is there a vectorstream class that implements the functionality similar to std::stringstream but with std::vector, not std::string? cheers, Marcin
9
by: Àî°× | last post by:
hi all: I want erase a stringstream' content for input new data to it. example: std::stringstream stm; stm<<"this is a string"; std::cout<<stm.str(); // here print:this is a string
14
by: cpisz | last post by:
I want to do some find and erase operations that i find in the string class, upon the entire text contents of a file. I made a function that will take a string (designed to hold the entire file...
10
by: vigacmoe | last post by:
I was trying to cast some strings to integers with stringstream, then this strange problem poped up. Here is my test code: stringstream conv; string from; int to; from = "1"; conv << from;
2
by: akitoto | last post by:
Hi there, I am using the std::stringstream to create a byte array in memory. But it is not working properly. Can anyone help me? Code: #include <vector> #include <sstream> #include...
4
by: clb | last post by:
I am trying to use stringstreams and my book doesn't cover the included methods. For example, if I init a istringstream on a string and suck all the data out, then put more stuff into the string,...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.