print a byte number with cout

M

Mastupristi

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 << " ";
cout.flags(old);
cout<<endl;
return 0;
}


I obtain:
0 0W 0ÿ 0ÿ

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

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);
printf("\n");
return 0;
}


so, how to print a byte numbe in hex format using cout?

thanks
 
J

Johan

HI,

First you have to cast tag to int;

for example

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

regards

Johan

"Mastupristi" <cialdi_NO_SP@AM_gmail.com> schreef in bericht
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 << " ";
cout.flags(old);
cout<<endl;
return 0;
}


I obtain:
0 0W 0ÿ 0ÿ

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

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);
printf("\n");
return 0;
}


so, how to print a byte numbe in hex format using cout?

thanks
 
D

Dietmar Kuehl

Johan said:
First you have to cast tag 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));

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').
 
O

Old Wolf

Mastupristi said:
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 << " ";


Change that to int(tag)
I obtain:
0 0W 0ÿ 0ÿ

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

I obtain:
01 57 FFFF FFFF
 
R

Raymond Martineau

[...]
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 << " "; [...]

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top