Sijith
Sijith

Reputation: 3956

convert unsigned integer to unsigned char *

I want to get the value from unsigned int to unsigned char *, my logic gives wrong value

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

Here m_Address is not gettting value 0x0a5b0644

Can I get some idea to do this

Upvotes: 0

Views: 1895

Answers (2)

aah134
aah134

Reputation: 860

you may try the follow assuming unsigned int is 4 bytes, {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

Upvotes: 0

Yu Hao
Yu Hao

Reputation: 122493

Converting from integer type to pointer will be implementation defined.

If you really need an unsigned integer type to store pointers, use uintptr_t(or intptr_tfor signed integer type) in <cstdint>. They are introduced in C++11 as an optional feature. They are capable of converting to void * type and converting back.

Upvotes: 2

Related Questions