473,857 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Formatted output


I've been beating my head against this for a little while, so perhaps
someone can help me out here? The code below should output exactly as
follows (the hex data lines up in a fixed font):

DATA 0123456789abcde f0123456789abcd ef
0123456789abcde f0123456789abcd ef

and the code does indeed do that when I use the fprintf() call. When
I comment that line out and use the iostream line, I get gibberish. I
guess I'm just not 'getting' C++ formatted I/O yet.

Here's the code, for what it's worth:

#include <cstdio>
#include <iostream>
#include <iomanip>

static void
output(const std::string& s, unsigned char* buf, const size_t len)
{
std::cerr << s << ' ';
for (unsigned int i = 0; i < len; ++i)
{
int j = (i+1) % 16;
// std::cerr << std::setw(2) << std::ios::hex << buf[i]; // no good
fprintf(stderr, "%02x", buf[i]); // works fine
if ((i+1) < len && j == 0)
std::cerr << '\n' << std::setw(s.siz e()) << ' ';
}
std::cerr << '\n';
}

int main()
{ unsigned char data[] =
{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};

output("DATA", data, 32);
exit(0);
}

Jul 26 '07 #1
4 4659
On 26 ec, 16:03, ke...@bytebroth ers.co.uk wrote:
I've been beating my head against this for a little while, so perhaps
someone can help me out here? The code below should output exactly as
follows (the hex data lines up in a fixed font):

DATA 0123456789abcde f0123456789abcd ef
0123456789abcde f0123456789abcd ef

and the code does indeed do that when I use the fprintf() call. When
I comment that line out and use the iostream line, I get gibberish. I
guess I'm just not 'getting' C++ formatted I/O yet.

Here's the code, for what it's worth:

#include <cstdio>
#include <iostream>
#include <iomanip>

static void
output(const std::string& s, unsigned char* buf, const size_t len)
{
std::cerr << s << ' ';
for (unsigned int i = 0; i < len; ++i)
{
int j = (i+1) % 16;
// std::cerr << std::setw(2) << std::ios::hex << buf[i]; // no good
fprintf(stderr, "%02x", buf[i]); // works fine
if ((i+1) < len && j == 0)
std::cerr << '\n' << std::setw(s.siz e()) << ' ';
}
std::cerr << '\n';

}

int main()
{ unsigned char data[] =
{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};

output("DATA", data, 32);
exit(0);

}
Try
std::cerr << std::setw(2) << std::ios::hex << (int)buf[i];

Jul 26 '07 #2
ke***@bytebroth ers.co.uk wrote:
I've been beating my head against this for a little while, so perhaps
someone can help me out here? The code below should output exactly as
follows (the hex data lines up in a fixed font):

DATA 0123456789abcde f0123456789abcd ef
0123456789abcde f0123456789abcd ef

and the code does indeed do that when I use the fprintf() call. When
I comment that line out and use the iostream line, I get gibberish. I
guess I'm just not 'getting' C++ formatted I/O yet.

Here's the code, for what it's worth:

#include <cstdio>
#include <iostream>
#include <iomanip>

static void
output(const std::string& s, unsigned char* buf, const size_t len)
{
std::cerr << s << ' ';
for (unsigned int i = 0; i < len; ++i)
{
int j = (i+1) % 16;
// std::cerr << std::setw(2) << std::ios::hex << buf[i]; // no good
That's your magic line:
std::cerr << std::setfill('0 ') << std::setw(2) << std::hex <<
static_cast<uns igned>(buf[i]) << std::setfill(' ');

Regards,

Zeppe
Jul 26 '07 #3
ke***@bytebroth ers.co.uk wrote:
>
I've been beating my head against this for a little while, so perhaps
someone can help me out here? The code below should output exactly as
follows (the hex data lines up in a fixed font):

DATA 0123456789abcde f0123456789abcd ef
0123456789abcde f0123456789abcd ef

and the code does indeed do that when I use the fprintf() call. When
I comment that line out and use the iostream line, I get gibberish. I
guess I'm just not 'getting' C++ formatted I/O yet.

Here's the code, for what it's worth:

#include <cstdio>
#include <iostream>
#include <iomanip>

static void
output(const std::string& s, unsigned char* buf, const size_t len)
{
std::cerr << s << ' ';
for (unsigned int i = 0; i < len; ++i)
{
int j = (i+1) % 16;
// std::cerr << std::setw(2) << std::ios::hex << buf[i]; // no good
First:
std::ios::hex is a constant. It's a flag, but this code just outputs the
number. Sending std::hex instead of std::ios::hex will fix this.
Second:
buf[i] is an unsigned char. Outputting it will call the overload for
unsigned char output. An explicit conversion to unsigned int would fix
this.

So:
std::cerr << std::setw(2) << std::hex << unsigned(buf[i]);
fprintf(stderr, "%02x", buf[i]); // works fine
If you're lucky. <cstdiodeclar es std::fprintf, and may declare ::fprintf.
You could say:
using std::fprintf;
to fix this issue. Or just use std::fprintf.
if ((i+1) < len && j == 0)
std::cerr << '\n' << std::setw(s.siz e()) << ' ';
}
std::cerr << '\n';
}

int main()
{unsigned char data[] =
{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};

output("DATA", data, 32);
exit(0);
}
--
rbh
Jul 26 '07 #4
On 26 Jul, 15:21, Zeppe <zep_p@.remove. all.this.long.c omment.yahoo.it >
wrote:
ke...@bytebroth ers.co.uk wrote:
// std::cerr << std::setw(2) << std::ios::hex << buf[i]; // no good

That's your magic line:
std::cerr << std::setfill('0 ') << std::setw(2) << std::hex <<
static_cast<uns igned>(buf[i]) << std::setfill(' ');
Thanks, guys!

Jul 26 '07 #5

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

Similar topics

12
2125
by: Craig Thomson | last post by:
Do people generally try and output nicely formatted html from their scripts? I have been trying to, with variable indenting for tables etc. It makes it easier to see the html and perhaps debug it, but it can make ugly looking scripts. What's the general consensus? Craig
2
1794
by: Mark | last post by:
How do I output formatted numerical data to an external (text) file. At a minimum I want to specify the number of decimal places to print. Ideally I'd like the type of control the fortran FORMAT statement offers. I'm sure this is easy, but I can't seem to find the answer! I want an output file to look like this: 0.1 0.8575 0.2 12.0900 0.4 345.0000
8
5218
by: Matthew Thorley | last post by:
Greetings, perhaps someone can explain this. I get to different styles of formatting for xmla and xmlb when I do the following: from elementtree import ElementTree as et xmla = et.ElementTree('some_file.xml') xmlb = et.Element('parent') et.SubElement(xmlb, 'child1') et.SubElement(xmlb, 'child2')
4
3582
by: Joe C | last post by:
I've written a console application and would like to isolate all screen output so that it will be easier to migrate the code to a GUI-type platform without modification to the base code. As a first step, I've created a variable: bool useConsole(true); then I trap all output like: if(useConsole){ cout << text; }
6
2206
by: Jason Heyes | last post by:
I am starting to worry about the performance of formatted read/write operations on data-redundant objects in my program.What can I do to improve this performance should it become an issue? Thanks.
2
3514
by: Steven T. Hatton | last post by:
I'm still not completely sure what's going on with C++ I/O regarding the extractors and inserters. The following document seems a bit inconsistent: http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#1 Copying a file: WRONG WAY: #include <fstream> std::ifstream IN ("input_file"); std::ofstream OUT ("output_file");
6
3002
by: Magix | last post by:
Hi, I want to use fprintf to write to a file. My question about the formatted output How can I format so that I can allocate certain width for each %s (Left-aignlied) ? Example: fprintf("%s %s %s %s, name, age, city, status); <-------------------><----------------><----------------><------------->
3
3381
by: Craig Petrie | last post by:
Hi, I have a large table in Word 2003 that has formatted text in the cells and wish to read and convert a cells formatted contents to html output via vb.net code. The formatting contains the following: bold text, italic text, superscript & subscript text, bullet points. The output html is then saved in an XML file as a CDATA section for later use in the application when formatted output is required. I have looked up many sites on the...
6
3692
by: Jojo | last post by:
Hi all, I was wondering how I can perform formatted output with C++ strings. For example, suppose I have in plain C: sprintf(C_string, "%5.2f %6d"); How can I do such a thing with C++ strings? Using a string stream and the << operator does not give me the formatting control like sprintf. Or did I miss something.... :-|
0
9768
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11083
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10808
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10394
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9549
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7104
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5774
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5971
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.