 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mastupristi Guest
|
Posted: Thu Feb 24, 2005 4:01 pm Post subject: 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<
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
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?
|
|
| Back to top |
|
 |
Johan Guest
|
Posted: Thu Feb 24, 2005 4:14 pm Post subject: Re: print a byte number with cout |
|
|
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"
news:20050224170131.000001f9.cialdi_NO_SP (AT) AM_gmail (DOT) 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<
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
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?
|
|
| Back to top |
|
 |
Dietmar Kuehl Guest
|
Posted: Thu Feb 24, 2005 5:52 pm Post subject: Re: print a byte number with cout |
|
|
Johan wrote:
| Quote: | 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
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:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
|
|
| Back to top |
|
 |
Old Wolf Guest
|
Posted: Thu Feb 24, 2005 10:58 pm Post subject: Re: print a byte number with cout |
|
|
Mastupristi wrote:
| Quote: | 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
#include
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.
| Quote: | 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])
| Quote: |
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
|
|
|
| Back to top |
|
 |
Raymond Martineau Guest
|
Posted: Thu Feb 24, 2005 11:00 pm Post subject: Re: print a byte number with cout |
|
|
On Thu, 24 Feb 2005 17:01:31 +0100, Mastupristi <cialdi_NO_SP (AT) AM_gmail (DOT) com>
wrote:
[...]
| Quote: |
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.
| Quote: |
I don't want to use printf, but with it could be simpler:
#include
|
It's best to use
global mainspace free from clutter.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|