简体   繁体   English

C ++,将reinterpret_cast结构*转换为无符号字符*

[英]c++, reinterpret_cast structure* to unsigned char*

At first I'm sorry for my English:) So, I have a structure and variable 起初,我为我的英语感到抱歉:)所以,我有一个结构和变量

typedef struct
{
  GHEADER  m_Header;
  BYTE    *m_Buf;
  Addr    *m_Abonent;
}__attribute__((packed)) PACKET;

unsigned char* uc_ptr;

I need to send to some function unsigned char pointer argument. 我需要向某些函数发送未签名的char指针参数。 I tried to use reinterpret_cast to cast a pointer to PACKET type to unsigned char* . 我试图使用reinterpret_cast将指向PACKET类型的指针转​​换为unsigned char*

PACKET* t_PACKET;
uc_ptr = reinterpret_cast<unsigned char*>(t_PACKET);

But then I tried 但是后来我尝试了

std::cout << *uc_ptr << std::endl;

I don't see anything. 我什么也没看到。 Why? 为什么? And how to cast this correctly? 以及如何正确投射呢?

When you use << to output a char you get a single character written to the output. 当使用<<输出char您将单个字符写入输出。 Many characters such as \\0 do not show up on the console. 诸如\\0类的许多字符不会显示在控制台上。

Try this instead to see what I mean: 试试这个代替我的意思:

std::cout << static_cast<unsigned int>(*uc_ptr) << std::endl;

You'll need a loop to get all of the bytes in the structure. 您将需要一个循环来获取结构中的所有字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM