Sprcificator %02X

Can I get this line in C++?
 
for (i = 0; i < 8; i++) printf("%02X", key[i]);
Well if i is already defined and key has at least 8 elements then this will work in c++ if the correct #include is used.

Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <iomanip>
#include <format>
#include <cstdio>

int main()
{
	constexpr int key[8] {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};

	// C way
	for (int i = 0; i < 8; ++i)
		std::printf("%02X", key[i]);

	std::putchar('\n');

	// Using C++ cout manipulators
	for (int i = 0; i < 8; ++i)
		std::cout << std::hex << std::setfill('0') << std::setw(2) << key[i];

	std::cout << '\n';

	// Using C++20 format
	for (int i = 0; i < 8; ++i)
		std::cout << std::format("{:02X}", key[i]);

	std::cout << '\n';
}



1122334455667788
1122334455667788
1122334455667788

Last edited on
yes, it works correctly. I mean can I get c++ style with cout?
See my edited post above.
Topic archived. No new replies allowed.