0
votes

How do I convert an unsigned int (UINT32) to a const char.

I tried to cast it but because isn’t type safe is crashing when the pointer is bigger than expected.

So how can I approach this correctly?

    unsigned int p = 0x32422342;
    strLocation = QString::fromUtf8((char*)p, sizeof(p));

BTW if I leave the cast, the compiler is showing up an exception:

cast to `const char*`from smaller integer type `UINT32` (aka `unsigned int`) [-Wint-to-pointer-cast]

any suggestions? The value is just a random number, just for clarification.

1
any suggestions? The value is just a random number, just for clarification - Leonoro
You are actually trying to cast a 32bit unsigned int to a pointer to char (char *), which, from the error message, seems to be wider than 32bits (64bits). - didierc
Note: If you have clarifications to make about the content of the question, please simply update the question body with those details, instead of putting them in comments. - didierc
What are you trying to achieve (not the cast itself, but on a broader context)? - leemes
Also, if you generate address out of random number, you're quite likely to end up on an invalid address (ie. an address which isn't in the memory space allocated by the program, or which cannot be read). - didierc

1 Answers

1
votes

Your pointers are probably 64 bits long, this is why your compiler complains... Are you sure that your 32 bits represents a good address ? Probably not.