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

时间:2013-06-28 14:42:58

标签: c++ c visual-c++

我想从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不是gettting值0x0a5b0644

我可以知道这样做吗

2 个答案:

答案 0 :(得分:2)

从整数类型转换为指针将是实现定义。

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

答案 1 :(得分:0)

您可以尝试以下操作 假设unsigned 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