将无符号整数转换为无符号char*

convert unsigned integer to unsigned char *

本文关键字:char 无符号 无符号整数 转换      更新时间:2023-10-16

我想得到从unsigned int到unsigned char*的值,我的逻辑给出了错误的值

unsigned int Address = 0x0a5b0644; // this value is getting ss fun parameter
m_Address        = (unsigned char*)Address;  // this logic wrote in side C++ file
unsigned char * m_Address; // this is declared inside H file

此处m_Address未获取值0x0a5b0644

我能想出一些做的主意吗

从整数类型到指针的转换将由实现定义。

如果确实需要无符号整数类型来存储指针,请在<cstdint>中使用uintptr_t(或intptr_t表示有符号整数类型)。它们在C++11中作为可选功能引入。它们能够转换为void *类型并转换回。

您可以尝试以下操作假设无符号int是4个字节,{0x0a,0x5b,0x06,0x44}

unsigned char bytes[11];// the number of charactoer in the string + a null terminating
sprintf(bytes, "%#04x", Address); //<here it will print it