Vala: Convert int to uint16 - gtk

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;

Related

How to convert / round a double value to int or String in flutter dart

here is a double value
final dblValue = 25.5;
and I want this number as this int or string.
I tried the replaceAll function to remove the value after the ., but that didn't work too, I just need the number only without. value. how do I do that in dart
You can use the .floor() method which goes to the int value without the decimal points. For a formal definition:
The greatest integer no greater than this number.
final d = 25.9;
final d2 = 25.1;
int i = d.floor(); // i = 25
int i2 = d2.floor(); // i2 = 25
If you then want it as a string you can use the .toString() method on the int you just did.
To convert it to a string use
dblValue.toString();
To round it use
dblValue.round();
Or
dblValue.toInt();
And to floor the value use
dblValue.floor();
You'll need to use the .floor() method to round up.
final dblValue = 25.5;
int value = d.floor(); // value = 25

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.

How to multiply a float by a bool value of 0 or 1?

My code below gives a breakpoint error:
func doScore(num: Float, binaural: Bool, noise: Bool) -> Float {
if 50 ... 100 ~= num{
let numDoubled = num + (Float(noise.intValue()!) * weighting)// <--- this is where I get my error
return numDoubled.rounded()
}
All I want to do is multiply the number I am putting into the function by the value of binaural or noise which are boolean values. To do this I am getting the Int value however I need it to be a float as 0 or 1 since the number I am putting in is a float. Why would this cause a crash? Thanks.
By doing a simple workaround, you can fix it easily
let numDoubled = num + (noise ? weighting : 0.0)
Converting bool into int is here but in my solution, there's no need to do double job( convert to int and then again convert into float)
Updated as per vacawama comment
Updated answer from your comment:
let numDoubled = num + ( (noise && binaural) ? weighting : 0.0 ) )
Unlike in (Objective-)C where nil, 0, and false are treated as the same value depending on the environment Int and Bool are not related in Swift and a Swift Bool doesn't have an intValue.
Practically you want to add weighting to num if noise is true.
let numDoubled = noise ? num + weighting : num
If you really need to convert false to 0 and true to 1 write
let boolAsInt = aBool ? 1 : 0
Even though implicit conversion between bool and numeric types has been removed, you can explicitly convert a bool to numeric types by first converting it to an NSNumber.
let numDoubled = num + Float(truncating: NSNumber(booleanLiteral: noise)) * weighting
You can also do Float(truncating: noise) as a shorthand notation, but that call implicitly converts the bool to an NSNumber, so for clarity its better to write out the conversion explicitly.

Difference between Int and Uint8 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
}

Unsigned long Int in Core data

I have an Entity with attribute of type Int64. But, I need to save an object with type Unsigned Long Int or unsigned long long Int. How can I do it?
Swift, please (:
Simply:
let x = Int64(5) //Int64
let y = CUnsignedLongLong(bitPattern: x) //Unsigned Long Long