In a program developed with C ++ on Linux I need to show the content of several arrays that contain bytes.
These arrays are declared as an array of characters.
I am trying to display its content with std::cout but I am not getting the output I need, lthough I have tried in several different ways.
I illustrate this with a simple program example:
#include <stdio.h>
#include <stdint.h>
#include <iostream>
#include <iomanip>
int main()
{
char data[10] = { 0 };
for (uint8_t i=0; i<10; i++)
data[i] = 5 + 2*i;
printf ("ONE: 0x%02X 0x%02X 0x%02X 0x%02X\n", data[2], data[3], data[4], data[5]);
std::cout << "TWO: " << std::setw(2) << std::hex << std::uppercase << std::left << std::setfill('0')
<< int(data[2]) << " " << int(data[3]) << " " << int(data[4]) << " " << int(data[5]) << std::endl;
std::cout << "THREE: "
<< std::setw(2) << std::hex << std::uppercase << std::left << std::setfill('0')
<< int(data[2]) << " "
<< std::setw(2) << std::hex << std::uppercase << std::left << std::setfill('0')
<< int(data[3]) << " "
<< std::setw(2) << std::hex << std::uppercase << std::left << std::setfill('0')
<< int(data[4]) << " "
<< std::setw(2) << std::hex << std::uppercase << std::left << std::setfill('0')
<< int(data[5]) << std::endl;
std::cout.setf(std::ios::hex | std::ios::left | std::ios::uppercase, std::ios::basefield);
std::cout.width(2);
std::cout.fill('0');
std::cout << "FOUR: " << int(data[2]) << " " << int(data[3]) << " "
<< int(data[4]) << " " << int(data[5]) << std::endl;
return 0;
}
The printf output is correct, it corresponds to the format I need:
ONE: 0x09 0x0B 0x0D 0x0F
The output I get in the TWO case is: TWO: 90 B D F The output I want to get is: TWO: 09 0B 0D 0F It presents the following errors:
- Only the first data of the array appears with a width of two characters. The others are shown with a width of one character.
- Value 9 is displayed as 90 when it should be 09 What should I change in this cout statement?
The TRHEE case ensures that the output of each data is always two characters wide: THREE: 90 B0 D0 F0 The output I want to get is: THREE: 09 0B 0D 0F It presents the following errors:
- Shows "0" on the right, instead of on the left.
- It is too cumbersome to have to set the exit indicators again before of displaying a new value in the same cout statement.
The FOUR case shows: FOUR: 9 B D F The output I want to get is: FOUR: 09 0B 0D 0F It presents the following errors:
- Does not show each data with two characters in width and a "0" on the left.
What is the correct way to use cout so that it shows 09 0B 0D 0F?
What should I change in the program?