473,382 Members | 1,809 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,382 software developers and data experts.

print a byte number with cout

I have an array of char

for example:
char tag[] = {1, 'W', 0xFF, 0xFF};

I want to obtain:
01 57 FF FF

if I try with:

#include <iostream>
#include <iomanip>
using namespace std;

static const char tag[] = {1, 'W', 0xFF, 0xFF};

int main(void)
{
ios::fmtflags old = cout.flags();
for(int i = 0; i< 4; i++) cout << hex << uppercase << setw(2) <<
setfill('0') <<
(unsigned char)tag[i] << " ";
cout.flags(old);
cout<<endl;
return 0;
}
I obtain:
0 0W 0ÿ 0ÿ

If I modify the cast from (unsigned char)tag[i] to (unsigned short)tag[i]

I obtain:
01 57 FFFF FFFF

I don't want to use printf, but with it could be simpler:
#include <stdio.h>

static const char tag[] = {1, 'W', 0xFF, 0xFF};

int main(void)
{
for(int i = 0; i< 4; i++) printf("%02hhX ",tag[i]);
printf("\n");
return 0;
}
so, how to print a byte numbe in hex format using cout?

thanks
--
Mastupristi?
Jul 23 '05 #1
4 14602
HI,

First you have to cast tag[i] to int;

for example

int val = tag[i];
cout << hex << setw(2) << setfill('0') << val << endl;

regards

Johan

"Mastupristi" <cialdi_NO_SP@AM_gmail.com> schreef in bericht
news:20050224170131.000001f9.cialdi_NO_SP@AM_gmail .com...
I have an array of char

for example:
char tag[] = {1, 'W', 0xFF, 0xFF};

I want to obtain:
01 57 FF FF

if I try with:

#include <iostream>
#include <iomanip>
using namespace std;

static const char tag[] = {1, 'W', 0xFF, 0xFF};

int main(void)
{
ios::fmtflags old = cout.flags();
for(int i = 0; i< 4; i++) cout << hex << uppercase << setw(2) <<
setfill('0') <<
(unsigned char)tag[i] << " ";
cout.flags(old);
cout<<endl;
return 0;
}
I obtain:
0 0W 0ÿ 0ÿ

If I modify the cast from (unsigned char)tag[i] to (unsigned short)tag[i]

I obtain:
01 57 FFFF FFFF

I don't want to use printf, but with it could be simpler:
#include <stdio.h>

static const char tag[] = {1, 'W', 0xFF, 0xFF};

int main(void)
{
for(int i = 0; i< 4; i++) printf("%02hhX ",tag[i]);
printf("\n");
return 0;
}
so, how to print a byte numbe in hex format using cout?

thanks
--
Mastupristi?
Jul 23 '05 #2
Johan wrote:
First you have to cast tag[i] to int;


For signed chars with negative values this actually does the wrong
thing: the sign is extended to fit the bigger integer type which
results in a value with all the higher bits being set. What the
original author has to do is to first cast the signed character to
an unsigned character and then to an appropriate integer type: when
casting to the unsigned character type, the bits remain unchanged
and for unsigned types there is no sign extension - obviously, since
they don't have a sign. That is, the code would look something like
this:

std::cout << int(static_cast<unsigned char>(tag[i]));

assuming the stream is setup appropriately to fit the desired
formatting.

BTW, note that you need to go through a similar procedure if you
want to use C's character classification function like 'isspace()':
these are only defined for unsigned character values but often the
type 'char' is signed (the standard makes not guarantee about the
signedness of 'char').
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #3
Mastupristi wrote:
I have an array of char

for example:
char tag[] = {1, 'W', 0xFF, 0xFF};

I want to obtain:
01 57 FF FF

if I try with:

#include <iostream>
#include <iomanip>
using namespace std;

static const char tag[] = {1, 'W', 0xFF, 0xFF};
Change this to unsigned char. 0xFF is not a valid char on your system.
int main(void)
{
ios::fmtflags old = cout.flags();
for(int i = 0; i< 4; i++) cout << hex << uppercase << setw(2) <<
setfill('0') <<
(unsigned char)tag[i] << " ";
Change that to int(tag[i])

I obtain:
0 0W 0ÿ 0ÿ

If I modify the cast from (unsigned char)tag[i] to (unsigned short)tag[i]
I obtain:
01 57 FFFF FFFF


Jul 23 '05 #4
On Thu, 24 Feb 2005 17:01:31 +0100, Mastupristi <cialdi_NO_SP@AM_gmail.com>
wrote:

[...]

static const char tag[] = {1, 'W', 0xFF, 0xFF};

int main(void)
{
ios::fmtflags old = cout.flags();
for(int i = 0; i< 4; i++) cout << hex << uppercase << setw(2) <<
setfill('0') <<
(unsigned char)tag[i] << " "; [...]
If I modify the cast from (unsigned char)tag[i] to (unsigned short)tag[i]

I obtain:
01 57 FFFF FFFF
This casts a signed char to an unsigned short, which results in the
undesired behaviour. The easiest way to fix this is to change tag[] to an
unsigned char array, but this may cause side-effects.

Another poster suggested a better method - cast to unsigned char, and cast
the result to an integer or short.

I don't want to use printf, but with it could be simpler:
#include <stdio.h>


It's best to use <cstdio> if you want to use printf() - it helps keep the
global mainspace free from clutter.
Jul 23 '05 #5

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

Similar topics

14
by: Marcin Ciura | last post by:
Here is a pre-PEP about print that I wrote recently. Please let me know what is the community's opinion on it. Cheers, Marcin PEP: XXX Title: Print Without Intervening Space Version:...
5
by: cybersangeeth | last post by:
Hi, I need to read 1KB each time from multiple files in a folder and pass it to a byte array in a struct to be sent through a socket. I'm a C++ newbie. I managed to read 1KB each time from one...
1
by: iwongu | last post by:
Hi, I have a question about std::wcout and its underlying C functions. According to C99, a stream have an orientation type that is one of byte-oriented or wide-one, and it is determined by the...
2
by: DBuss | last post by:
OK, I'm reading a multicast socket. It attaches fine, reads fine, all of that. The problem is that while some of the data I get is normal text (ASCII String), some of it is Binary Integer. ...
6
by: devoreband | last post by:
hey i wrote all my code but i need to print out only the even numbers so is there any predefined function i can use or what can structure i should be following? ........ 16 for(int r = 0;...
4
by: j_depp_99 | last post by:
Thanks to those guys who helped me out yesterday. I have one more problem; my print function for the queue program doesnt work and goes into an endless loop. Also I am unable to calculate the...
29
by: Virtual_X | last post by:
As in IEEE754 double consist of sign bit 11 bits for exponent 52 bits for fraction i write this code to print double parts as it explained in ieee754 i want to know if the code contain any...
3
by: Hypnotik | last post by:
Hello everyone. This is a relatively simple program that is driving me crazy. The user enters how many verticies and how many edges. The program then asks where the edges are...in a 2D array. If the...
8
by: andrew.smith.cpp | last post by:
Hello how Can i get this kind of output? 1,2,3,5,8,13,21 i try it with loop but could print it. Thanks
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.