2013-06-28 178 views
0

我想從unsigned int類型爲unsigned char *的價值,我的邏輯給出錯誤的值轉換爲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

我能得到一些想法做到這一點

+1

_is_它得到什麼樣的價值? – Chowlett

+0

嗯,這可能是['reinterpret_cast <>'](http://en.cppreference.com/w/cpp/language/reinterpret_cast)會有幫助嗎? – JBL

+0

我得到一些垃圾值0xFFFF80 – Sijith

回答

2

從整數類型轉換爲指針將實現定義。

如果您確實需要一個無符號整數類型來存儲指針,請在<cstdint>中使用uintptr_t(或intptr_t作爲有符號整數類型)。作爲可選功能,它們在C++ 11中引入。他們能夠轉換爲void *類型並轉換回來。

+0

@Griwes:他們是。請參見C11的§7.20.1.4(與C99中的相同)。 – md5

+0

@Griwes查看http://www.cplusplus.com/reference/cstdint/ –

0

您可以嘗試跟隨 假設unsigned int類型是4個字節,{0X0A,0x5b,0×06,×44}

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