How to convert the byte data of Int32 to UInt32 and back?

2.5k views Asked by At

Academically Natured Question:
How can the byte data of an Int32 with the value of -1 be cast into an UInt32? (can SWIFT do that?)

Understanding:
I know that -1 isn't a value that can be represented by an Unsigned Integer, as UInts only contain Integers above -1

However I also know that both Int32 and UInt32 take the same amount of space in Bytes (4*8=32).
That byte space should be able to be used for either type, regardless if it represents the same value... which it obviously wouldn't.

Conclusion:
There should be some simple way to take the raw bit data of an Int32 and use it for a UInt32...

1

There are 1 answers

1
AudioBubble On BEST ANSWER

Cast the variable via bitPattern (thanks Jthora). Plenty of help here on SO for this:

For 32-bits, 0xffffffff=> -1 when signed or 4294967295 unsigned.