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

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...

Cast the variable via bitPattern (thanks Jthora). Plenty of help here on SO for this:
Converting signed to unsigned in Swift
Int to UInt (and vice versa) bit casting in Swift
For 32-bits, 0xffffffff=> -1 when signed or 4294967295 unsigned.

Related

NSCoder for unsigned integers with swift

In my swift app, I really need to store a UInt32 value and retrieve it using NSCoder.
The issue is, there are methods for signed integers :
coder.decodeInteger()
coder.decodeInt32()
coder.decodeInt64()
but not for unsigned integers.
Also, casting an Int32 of negative value in an UInt32 does not seem to work.
Am I missing some point?
The tool you want is init(bitPattern:). This is the same as C-style casting on integers (which is how you use these methods in ObjC when you need unsigned ints). It just reinterprets the bits.
UInt32(bitPattern: coder.decodeInt32())

Why an Int32 variable can't be assigned to an Int64 variable or vice versa in Swift?

I could not assign Int32 var/let to Int64 var/let or vice-versa in Swift. I get a compile time error when I attempt to do so. What is the reason behind this ?
If you declare a variable as Int32, the memory allocated for that variable is 4bytes and for Int64 type variable the memory allocated is 8 bytes.
You can't put 64bytes data into 32 byte data as you can't put 2litres of water into 1 litre bottle.
In order to avoid such problems, swift has strict type checking feature and ensure such problems are not arise in run time.

Why are random generated numbers UInt-32 by default in Swift?

I stumbled upon the fact that when a random number is generated in Swift, it is of type UInt32 by default instead of type Int
What's the reason?
I suspect you are referring to arc4random. This function returns UInt32 because the underlying C function (also called arc4random) returns uint32_t, which is the C equivalent of Swift's UInt32.
I would assume it makes it faster. If you want Int random numbers take a look at GKRandomSource in the game kit, https://developer.apple.com/documentation/gameplaykit/gkrandomsource

atoi() is not converting properly

I was trying to call atoi on the strings 509951644 and 4099516441. The first one got converted without any problem. The second one is giving me the decimal value 2,147,483,647 (0x7FFFFFFF). Why is this happening?
Your second integer is creating an overflow. The maximum 32-bit signed integer is 2147483647.
It's generally not recommended to use atoi anyway; use strtol instead, which actually tells you if your value is out of range. (The behavior of atoi is undefined when the input is out of range. Yours seems to be simply spitting out the maximum int value)
You could also check if your compiler has something like a atoi64 function, which would let you work with 64-bit values.
2147483647 is the maximum integer value in C (signed). It is giving the max that it can... the original is too large to convert to signed int. I suggest looking up how to convert into an unsigned int.

iPhone int parsing

I'm working on an iPhone app. I want to parse a series of numbers from a string. However, intValue is acting really really strange.
I have a string with the value 1304287200000.
I then place the intValue of that into an NSInteger, and lo and behold, my integer is for some reason assigned the value of 2147483647.
What gives?
The datatype int is a 32bit numeric value, with a range of approximately ±2 billion. 1304287200000 is by a margin outside of that range.
You need to skip int for long long that is a 64bit type and covers your need. A more human readable and explicit name for the 64bit type is int64_t.
What you are getting back is INT32_MAX, because the parsed value overflows the int type. This is explained in the NSString documentation. Try longLongValue instead, LLONG_MAX should be big enough.
int is 32-bit, so the maximum value it can hold is 2,147,483,647. Try longLongValue instead.
Your number exceeding integer limits