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
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