473,473 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to read/write continuous hex dump using STL

Hello all,

The program below has a bug... The program is supposed to convert a
string to a continuous hex dump and back to a string. The output
should be as follows:

abcd
61626364
abcd

But the output I see is as follows:

abcd
61626364
dddd

Can someone please show me what I'm doing wrong and what is the proper
way of reading a continuous hex dump using the STL?

// BEGIN SAMPLE PROGRAM
#include <iostream // For std::cout
#include <string // For std::string
#include <sstream // For std::ostringstream
#include <iomanip // For std::setfill

int
main()
{
// Set "input"
//
std::string input = "abcd";
std::cout << input << std::endl;

// Set "hexDump"
//
std::ostringstream oss;
oss << std::hex << std::setfill('0');
for(int i = 0; i < input.length(); i++)
{
int temp = input[i];
oss << std::setw(2) << temp;
}
std::string hexDump = oss.str();
assert(hexDump.length() == 2 * input.length());
std::cout << hexDump << std::endl;

// Set "output"
//
std::string output;
int numChars = hexDump.length() / 2;
output.resize(numChars);
std::istringstream iss(hexDump);
iss >std::hex;
for(int i = 0; i < numChars; i++)
{
int temp;
iss >std::setw(2) >temp; // BUG: Using setw DOES NOT WORK!!!
output[i] = temp;
}
std::cout << output << std::endl;
}
// END SAMPLE PROGRAM

Jun 30 '07 #1
7 5661

<ca******@yahoo.comwrote in message
news:11**********************@k29g2000hsd.googlegr oups.com...
Hello all,

The program below has a bug... The program is supposed to convert a
string to a continuous hex dump and back to a string. The output
should be as follows:

abcd
61626364
abcd

But the output I see is as follows:

abcd
61626364
dddd

Can someone please show me what I'm doing wrong and what is the proper
way of reading a continuous hex dump using the STL?

// BEGIN SAMPLE PROGRAM
#include <iostream // For std::cout
#include <string // For std::string
#include <sstream // For std::ostringstream
#include <iomanip // For std::setfill
#include <cassert // for assert
>
int
main()
{
// Set "input"
//
std::string input = "abcd";
std::cout << input << std::endl;

// Set "hexDump"
//
std::ostringstream oss;
oss << std::hex << std::setfill('0');
for(int i = 0; i < input.length(); i++)
{
int temp = input[i];
oss << std::setw(2) << temp;
}
std::string hexDump = oss.str();
assert(hexDump.length() == 2 * input.length());
std::cout << hexDump << std::endl;

// Set "output"
//
std::string output;
int numChars = hexDump.length() / 2;
output.resize(numChars);
std::istringstream iss(hexDump);
iss >std::hex;
for(int i = 0; i < numChars; i++)
{
int temp;
iss >std::setw(2) >temp; // BUG: Using setw DOES NOT WORK!!!
// Not true. 'setw' sets *output* width
*/

Had you checked the stream state of 'iss' (which should always be
done with any stream), you'd have seen the conversion failed.
The '>>' operator stops reading upon encountering whitespace.
So it tried to read hex value '6162636465', which is
418262508645 in decimal, most likely outside the guaranteed range
of type 'int' for your platform (32 bits?).

You'll need to either insert whitespace between your hex values,
or only read two characters at a time (you could use 'iss.get()'
or 'iss.read()'.
output[i] = temp;
}
std::cout << output << std::endl;
}
// END SAMPLE PROGRAM
-Mike
>

Jun 30 '07 #2
Hello Mike,

Thanks for the quick reply!
You'll need to either insert whitespace between your hex values,
Yes, I have a version working which inserts whitespace, but I need
a continuous hex dump.
// Not true. 'setw' sets *output* width
Yes, apparently so... but my reference book on STL says it sets
the *input* width as well. I am using "The C++ Standard Library:
A Tutorial and Reference" by Nicolai M. Josuttis, August 2002,
and on page 619, Table 13.16 he says: "setw(val) Sets the field
width for input and output to val" Can people confirm whether this
is a typo on the Josuttis book? (I've noticed several others).

Thanks!

Jun 30 '07 #3
On Jun 30, 12:32 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
The '>>' operator stops reading upon encountering whitespace.
So it tried to read hex value '6162636465', which is
418262508645 in decimal, most likely outside the guaranteed range
of type 'int' for your platform (32 bits?).
This post is for the benefit of the archive and anybody who reads
this thread in the future. I believe Mike recompiled the program
and used "abcde" as input, instead of "abcd". That is the reason
why he says that the program tried to read hex value '6162636465'.
The original program tries to read hex value '61626364' which is
32 bits. Thanks for actually compiling the program Mike!

Jun 30 '07 #4
On 2007-06-30 19:59, ca******@yahoo.com wrote:
Hello Mike,

Thanks for the quick reply!
>You'll need to either insert whitespace between your hex values,
Yes, I have a version working which inserts whitespace, but I need
a continuous hex dump.
>// Not true. 'setw' sets *output* width
Yes, apparently so... but my reference book on STL says it sets
the *input* width as well. I am using "The C++ Standard Library:
A Tutorial and Reference" by Nicolai M. Josuttis, August 2002,
and on page 619, Table 13.16 he says: "setw(val) Sets the field
width for input and output to val" Can people confirm whether this
is a typo on the Josuttis book? (I've noticed several others).
setw() will call width() on the istream, and width() only controls the
width of output, the reason istream has it is because it's inherited
from ios_base.

--
Erik Wikström
Jun 30 '07 #5

<ca******@yahoo.comwrote in message...
Hello Mike,
You'll need to either insert whitespace between your hex values,
Yes, I have a version working which inserts whitespace, but I need
a continuous hex dump.
Well, whitespace makes it easier. <G>
Just in case, this works with your original set-up:

{ // ....... your main, up to here.
// - Set "output" -
std::string output;
std::istringstream iss( hexDump ); // "61626364"
// std::istringstream Tiss( oss.str() ); // alt. way
// std::cout<< Tiss.str() <<std::endl; // out: 61626364

std::stringstream iss2;
for( std::string temp( 4, ' ' ); // or: 2
iss.read( &temp.at(0), 2 ); /*m t*/){
iss2.str( temp );
int tmp(0);
iss2 >std::hex >tmp; // if( not tmp ){ break;}
output += char( tmp & 0xFF ); // or: '& 0x7F'
} // for(iss.read)
std::cout << output << std::endl;
}
// out:abcd
This loop just reads two chars at a time, then (re-)converts them.
Hope it helps you in some way.

BTW, your original post == very good posting. Makes it easy to help you when
good information is provided, and code style is easy to read and complete.
You get an "atta boy(or girl)!".
--
Bob R
POVrookie
Jun 30 '07 #6

BobR wrote in message...
>
for( std::string temp( 4, ' ' ); // or: 2
Ooops, typo, two won't work, make that '// or: 3'. <G>
iss.read( &temp.at(0), 2 ); /*m t*/){
<snip>
} // for(iss.read)
std::cout << output << std::endl;
}
--
Bob R
POVrookie
Jun 30 '07 #7
<ca******@yahoo.comwrote in message
news:11**********************@n2g2000hse.googlegro ups.com...
On Jun 30, 12:32 pm, "Mike Wahler" <mkwah...@mkwahler.netwrote:
>The '>>' operator stops reading upon encountering whitespace.
So it tried to read hex value '6162636465', which is
418262508645 in decimal, most likely outside the guaranteed range
of type 'int' for your platform (32 bits?).
This post is for the benefit of the archive and anybody who reads
this thread in the future. I believe Mike recompiled the program
and used "abcde" as input, instead of "abcd". That is the reason
why he says that the program tried to read hex value '6162636465'.
Actually, I compiled if almost it as you posted it, but with the addition
of #include <cassert>, and the check of 'iss' stream state.
The original program tries to read hex value '61626364' which is
32 bits.
Actually, my error was that I simply typed the
(wrong) hex value into a calculator and converted to decimal.

I just now made a few more modifications to the code to 'watch' it and
realized:
The real reason for the fail state of 'iss' was that the entire
hex value (0x61626364) was read into 'temp' on the first loop
iteration, and all other reads cause an end of stream condition
(which also sets 'failbit'). That's why every character of the
string 'output' had this same value. (Casting this value to char
(Win XP, Pentium, gives character 'd').

Modified code: (my changes marked /* MKW */)

#include <iostream // For std::cout
#include <string // For std::string
#include <sstream // For std::ostringstream
#include <iomanip // For std::setfill
#include <cassert /* MKW */
int
main()
{
// Set "input"
//
std::string input = "abcd";
std::cout << input << std::endl;

// Set "hexDump"
//
std::ostringstream oss;
oss << std::hex << std::setfill('0');
for(int i = 0; i < input.length(); i++)
{
int temp = input[i];
oss << std::setw(2) << temp;
}
std::string hexDump = oss.str();
assert(hexDump.length() == 2 * input.length());
std::cout << hexDump << std::endl;

// Set "output"
//
std::string output;
int numChars = hexDump.length() / 2;
output.resize(numChars);
std::istringstream iss(hexDump);
iss >std::hex;
for(int i = 0; i < numChars; i++)
{
int temp;
iss >std::setw(2) ;
if(!iss) /* MKW */
std::cerr << "iss error on setw\n"; /* MKW */
else /* MKW */
std::cerr << "OK on setw\n"; /* MKW */

iss >temp; // BUG: Using setw DOES NOT WORK!!!
if(!iss) /* MKW */
std::cerr << "iss error on temp\n"; /* MKW */
else /* MKW */
{ /* MKW */
std::cerr << "OK on temp\n"; /* MKW */
std::cerr << " (temp == " << temp << ")\n"; /* MKW */
} /* MKW */
output[i] = temp;
}
std::cout << output << std::endl;

std::cout << char(0x61626364) << '\n'; /* MKW */
}

Thanks for actually compiling the program Mike!
Output:

abcd
61626364
OK on setw
OK on temp
(temp == 1633837924)
OK on setw
iss error on temp
iss error on setw
iss error on temp
iss error on setw
iss error on temp
dddd
d
.... so in my haste, I did err in my analysis. The stream
state was indeed 'fail', but not for the reason I originally
stated.

My apologies for any confusion.

But I still stand by my advice to always check stream
state after all operations.

-Mike
Jun 30 '07 #8

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

Similar topics

2
by: Tony Johansson | last post by:
Hello Experts!! I have two small classes called Intvektor and Matris shown at the bottom and a main. Class Intvektor will create a one dimension array of integer by allocate memory dynamically...
4
by: Kathy | last post by:
What is the standard technique for handling the fields in the following scenario on a continuous form? Multiple Divisions. Each Division has multiple Buildings. Each Building has a Supervisor. ...
9
by: hiralparikh | last post by:
Hi, I am using .NET 2.0 and trying to use a function from a native DLL file. Here is the syntax that I am using: definition: public static extern String getPwd(String strServerName, String...
12
by: Sean Davis | last post by:
I am working on a simple script to read from one database (oracle) and write to another (postgresql). I retrieve the data from oracle in chunks and drop the data to postgresql continuously. The...
0
by: ttamilvanan81 | last post by:
Hai, I am doing a Struts application. In this application, i need to read a dbf file(foxpro database file) and it will be write to the SQL Server 2005. The application administrator will be...
6
by: Greg Strong | last post by:
Hello All, Is is possible to use an ADO recordset to populate an unbound continuous Subform? I've done some Googling without much luck, so this maybe impossible, but let me try to explain...
9
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
3
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have an xsd that I want to save as an XML string to store in a DB I can save as a physical file using xsd.WriteXml(@"C:\Temp\Junk\junk.xml"); But I am unable to save to a string so I...
4
by: =?Utf-8?B?U2NvdHRSYWREZXY=?= | last post by:
So I have this problem. I'm currently involved in a team project that has a nightly build on a build server and a continuous integration build on the same server. We want to build our c# program...
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
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
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.