以十六进制表示法将字节缓冲区的字节打印到控制台输出流(0xABCDEF)

som*_*lse -3 c c++ hex types bytebuffer

我知道有很多关于这个主题的问题,但我认为找不到合适的关键字,所以我问.

我想打印出一个字节缓冲区的字节到十六进制表示法(0xABCDEF)的控制台输出,但我不知道什么是字节缓冲区,它用于什么?

我需要以下的东西,我只是一个初学者,所以请简单,我可以得到.(在c/c ++中)

@param pBytes指向字节缓冲区的指针@param nBytes字节缓冲区的长度,以字节为单位

void PrintBytes(const char* pBytes, const uint32_t nBytes);
Run Code Online (Sandbox Code Playgroud)

我需要那些功能.

你不需要给出我需要你的答案,让我更容易!谢谢!

Nik*_*lis 7

使用C++,您可以执行以下操作:

#include <iostream>
#include <iomanip>

void PrintBytes(
    const char* pBytes,
    const uint32_t nBytes) // should more properly be std::size_t
{
    for (uint32_t i = 0; i != nBytes; i++)
    {
        std::cout << 
            std::hex <<           // output in hex
            std::setw(2) <<       // each byte prints as two characters
            std::setfill('0') <<  // fill with 0 if not enough characters
            static_cast<unsigned int>(pBytes[i]) << std::endl;
    }
}
Run Code Online (Sandbox Code Playgroud)