c++ - How to print bytes as hexadecimal?

C++ - How to print bytes as hexadecimal?

To print bytes as hexadecimal in C++, you can use the std::hex stream manipulator along with std::setw to set the width of each hexadecimal byte. Here's a simple example:

#include <iostream> #include <iomanip> int main() { unsigned char bytes[] = {0x1A, 0xB4, 0xC8, 0x5F, 0x02}; // Print bytes as hexadecimal for (size_t i = 0; i < sizeof(bytes); ++i) { std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(bytes[i]) << " "; } std::cout << std::endl; return 0; } 

In this example:

  • std::hex sets the output stream to hexadecimal mode.
  • std::setw(2) sets the width of each hexadecimal byte to 2 characters.
  • std::setfill('0') ensures that leading zeros are added if needed.
  • static_cast<int>(bytes[i]) converts the byte to an integer before printing.

Adjust the array bytes to contain the bytes you want to print in hexadecimal format. This example prints each byte followed by a space.

Output:

1a b4 c8 5f 02 

Note that the << std::hex manipulator affects the entire stream, so if you want to switch back to decimal or other formatting, you might need to use << std::dec or other manipulators accordingly.

Examples

  1. "C++ print bytes as hexadecimal using iostream"

    • Code Implementation:
      #include <iostream> #include <iomanip> int main() { char byteArray[] = {0x2A, 0x7F, 0xC8, 0x01}; // Print bytes as hexadecimal for (const auto &byte : byteArray) { std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte) << " "; } return 0; } 
    • Description: Utilizes std::hex, std::setw, and std::setfill from <iomanip> to print each byte in the array as a hexadecimal value.
  2. "C++ print bytes as hexadecimal using printf"

    • Code Implementation:
      #include <cstdio> int main() { char byteArray[] = {0x2A, 0x7F, 0xC8, 0x01}; // Print bytes as hexadecimal using printf for (const auto &byte : byteArray) { printf("%02X ", static_cast<unsigned char>(byte)); } return 0; } 
    • Description: Uses printf with the %02X format specifier to print each byte in the array as a hexadecimal value.
  3. "C++ print bytes as hexadecimal with delimiter using iostream"

    • Code Implementation:
      #include <iostream> #include <iomanip> int main() { char byteArray[] = {0x2A, 0x7F, 0xC8, 0x01}; // Print bytes as hexadecimal with delimiter for (const auto &byte : byteArray) { std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte) << "-"; } return 0; } 
    • Description: Similar to the first example but adds a delimiter ("-") between each hexadecimal byte value.
  4. "C++ print bytes as hexadecimal with new line using iostream"

    • Code Implementation:
      #include <iostream> #include <iomanip> int main() { char byteArray[] = {0x2A, 0x7F, 0xC8, 0x01}; // Print bytes as hexadecimal with a new line after each byte for (const auto &byte : byteArray) { std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte) << "\n"; } return 0; } 
    • Description: Similar to the first example but adds a new line after printing each hexadecimal byte value.
  5. "C++ print bytes as hexadecimal with leading '0x' using iostream"

    • Code Implementation:
      #include <iostream> #include <iomanip> int main() { char byteArray[] = {0x2A, 0x7F, 0xC8, 0x01}; // Print bytes as hexadecimal with leading '0x' for (const auto &byte : byteArray) { std::cout << "0x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte) << " "; } return 0; } 
    • Description: Adds the "0x" prefix before each hexadecimal byte value.
  6. "C++ print bytes as hexadecimal using stringstream"

    • Code Implementation:
      #include <iostream> #include <sstream> #include <iomanip> int main() { char byteArray[] = {0x2A, 0x7F, 0xC8, 0x01}; // Print bytes as hexadecimal using stringstream std::ostringstream oss; for (const auto &byte : byteArray) { oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte) << " "; } std::cout << oss.str(); return 0; } 
    • Description: Uses a std::ostringstream to accumulate the hexadecimal byte values before printing them.
  7. "C++ print bytes as hexadecimal with std::vector"

    • Code Implementation:
      #include <iostream> #include <iomanip> #include <vector> int main() { std::vector<char> byteArray = {0x2A, 0x7F, 0xC8, 0x01}; // Print bytes as hexadecimal from a std::vector for (const auto &byte : byteArray) { std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte) << " "; } return 0; } 
    • Description: Demonstrates how to print bytes as hexadecimal when they are stored in a std::vector.
  8. "C++ print bytes as hexadecimal using range-based for loop"

    • Code Implementation:
      #include <iostream> #include <iomanip> int main() { char byteArray[] = {0x2A, 0x7F, 0xC8, 0x01}; // Print bytes as hexadecimal using range-based for loop for (auto byte : byteArray) { std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte) << " "; } return 0; } 
    • Description: Utilizes a range-based for loop for a concise way to print bytes as hexadecimal.
  9. "C++ print bytes as hexadecimal with reverse order"

    • Code Implementation:
      #include <iostream> #include <iomanip> int main() { char byteArray[] = {0x2A, 0x7F, 0xC8, 0x01}; // Print bytes as hexadecimal in reverse order for (int i = sizeof(byteArray) - 1; i >= 0; --i) { std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byteArray[i]) << " "; } return 0; } 
    • Description: Prints bytes as hexadecimal in reverse order, starting from the last byte in the array.
  10. "C++ print bytes as hexadecimal with no delimiter"

    • Code Implementation:
      #include <iostream> #include <iomanip> int main() { char byteArray[] = {0x2A, 0x7F, 0xC8, 0x01}; // Print bytes as hexadecimal with no delimiter for (const auto &byte : byteArray) { std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte); } return 0; } 
    • Description: Prints bytes as hexadecimal with no delimiter between them.

More Tags

geojson x86-64 m appsettings core-audio powershell-cmdlet jsonschema median C idp

More Programming Questions

More Electronics Circuits Calculators

More Transportation Calculators

More Organic chemistry Calculators

More Tax and Salary Calculators