Difference between Int and Uint8 swift - swift

What are the differences between the data types Int & UInt8 in swift.
Looks like UInt8 is used for binary data, i need to convert UInt8 to Int is this possible.

That U in UInt stands for unsigned int.
It is not just using for binary data. Uint is used for positive numbers only, like natural numbers.
I recommend you to get to know how negative numbers are understood from a computer.

Int8 is an Integer type which can store positive and negative values.
UInt8 is an unsigned integer which can store only positive values.
You can easily convert UInt8 to Int8 but if you want to convert Int8 to UInt8 then make sure value should be positive.

UInt8 is an 8bit store, while Int not hardly defined or defined by the compiler:
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html
Int could be 32 or 64 bits

Updated for swift:
Operation Output Range Bytes per Element
uint8 0 to 255 1
Int - 9223372036854775808 to 9223372036854775807 2 or 4
If you want to find the max and min range of Int or UInt8:
let maxIntValue = Int.max
let maxUInt8Value = UInt8.max
let minIntValue = Int.min
let minUInt8Value = UInt8.min
If you want to convert UInt8 to Int, used below simple code:
func convertToInt(unsigned: UInt) -> Int {
let signed = (unsigned <= UInt(Int.max)) ?
Int(unsigned) :
Int(unsigned - UInt(Int.max) - 1) + Int.min
return signed
}

Related

same Int has different values in Swift, mysterious riddle

with this code :
let rand : Int = Int(arc4random())
NSLog("rand = %d %i %# \(rand)",rand,rand,String(rand))
I get :
rand = -1954814774 -1954814774 2340152522 2340152522
why all 4 values are not the same ?
arc4random generates an unsigned 32bit int. Int is probably 64 bit on your machine so you get the same number and it doesn't overflow. But %i and %d are signed 32-bit format specifiers. See here and here. That's why you get a negative number when arc4random returns a number greater than 2^32-1, aka Int32.max.
For example, when 2340152522 is generated, you get -1954814774 in the %i position because:
Int32(bitPattern: 2340152522) == -1954814774
On the other hand, converting an Int to String won't change the number. Int is a signed 64 bit integer.

In Swift I cant create a negative number in binary

I'm trying to assign -5 to signedInt but getting an error:
Integer literal '133' overflows when stored into 'Int8'
let signedInt: Int8 = 0b10000101
print(signedInt)
print(String(signedInt, radix: 2))
Your value is not -5, but -123.
You can't get there with a direct assignment because the value is a signed Int and interpreted as 133.
To assign a negative value, use Int8(bitpattern:) to convert the value from a UInt8 to an Int8:
let signedInt = Int8(bitPattern: 0b10000101)
print(signedInt)
-123
-5 is 0b11111011 which is the 2's complement of 0b00000101.
To form the 2's complement, start with the binary pattern for 5:
0b00000101
invert all of the bits:
0b11111010
and add 1:
0b11111011
You can use UInt8(bitPattern:) to find the representation of the number:
let signedInt: Int8 = -5
print(String(UInt8(bitPattern: signedInt), radix: 2))
11111011

How to convert int into Uint32 in flutter

Is there a way to convert an integer into the format Uint32.
int x = 94;
Uint32 y = x.toUint32(); ///Pseudocode
Dart doesn't have a unsigned integers.
You can however use a BigInt and use toUnsigned(32) after each operation you do to it.

Swift Binary string to Int [duplicate]

I'm currently trying to use the function
Int(binaryString, radix: 2)
to convert a string of binary to an Int. However, this function seems to always be converting the binary string into an unsigned integer. For example,
Int("1111111110011100", radix: 2)
returns 65436 when I'd expect to get -100 from it if it were doing an signed int conversion. I haven't really worked with binary much, so I was wondering what I should do here? Is there a code-efficient way built-into Swift3 that does this for signed ints? I had initially expected this to work because it's an Int constructor (not UInt).
Playing around you can get the desired result as follows:
let binaryString = "1111111110011100"
print(Int(binaryString, radix: 2)!)
print(UInt16(binaryString, radix: 2)!)
print(Int16(bitPattern: UInt16(binaryString, radix: 2)!))
Output:
65436
65436
-100
The desired result comes from creating a signed Int16 using the bit pattern of a UInt16.

Vala: Convert int to uint16

How do I convert from int to uint16 in vala?
My requirement is to convert the text in decimal in a gtkEntry to uint16.
How do I do this?
Just cast. uint16 as_uint16 = (uint16) as_int;
You may want to first check that the value is between uint16.MIN and uint16.MAX.
int myint = 543;
uint16 z = 0 + myint;