How to print out the contents of a PBYTE?

北慕城南 提交于 2020-12-15 05:38:06

问题


I understand PBYTE is unsigned char* from Windows Data Types.

I want to be able to print all the contents, either using printf() or cout:

PBYTE buffer;
ULONG buffersize = NULL;
serializeData(data, buffer, buffersize); 
//this function serializes "data" and stores the data in buffer and updates bufferSize).. 

Can you help me understand how to print this in C++?


回答1:


If you want to print the memory address that buffer is pointing at, then you can do it like this:

PBYTE buffer;
ULONG buffersize = 0;
serializeData(data, buffer, buffersize); 
//this function serializes "data" and stores the data in buffer and updates bufferSize)...

printf("%p", buffer);
or
std::cout << (void*)buffer;

...

But, if you want to print the contents of the buffer, you need to loop through the buffer and print out each BYTE individually, eg:

PBYTE buffer;
ULONG buffersize = 0;
serializeData(data, buffer, buffersize); 
//this function serializes "data" and stores the data in buffer and updates bufferSize)...

for (DWORD i = 0; i < buffersize; ++i)
{
    printf("%02x ", buffer[i]);
    or
    std::cout << std::hex << std::setw(2) << std::setfill('0') << (unsigned int)buffer[i] << " ";
}

...


来源:https://stackoverflow.com/questions/64378866/how-to-print-out-the-contents-of-a-pbyte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!