How to convert UInt8 to bytes Swift 4 - swift

How do I convert 4 to 0x04 in Swift 4?
var value: UInt8 = 4 // want to input 4
let bytes = NSData(bytes: &value, length: sizeof(UInt8())) // bytes must = 0x04

If "bytes must = 0x04" it sounds like you want a Data with a single byte with a value of 4, right? The code you have written will produce an NSData with an array of bytes that represents the memory address of value.
If you just want a single byte in your data, you can say Data(bytes: [value]).

Related

Dart Convert two Uint8 to Uint16 that are little endian

I am new to dart but I need to take two Uint8 (part of a bluetooth response) and convert them to a single Uint16. They are also in little endian (LSB) so the second value will need to shift 8 bytes. I am struggling on how to do this in Dart.
I have tried something like this but it isn't coming close as the values are too high.
var list = new Uint8List(2);
list[0] = 56;
list[1] = 55;
int intValue = list[0] + (list[1] << 8);
Uint16 int16Value = Uint16(intValue);
print(int16Value);
Thank you very much.
You can get the ByteData from the list and ByteData has all sorts of functions for changing endianess and getting different integer types, more here.
var data = list.buffer.asByteData();
print(data.getUint16(0, Endian.little));

How to convert byte position in swift

How would I convert this properly? the value I need is byte number 0-1, has a format of uint16 and its units are in degrees.
print("derived : \(characteristic.value!)")
print(String(bytes: characteristic.value!, encoding: .utf16))
derived : 20 bytes
Optional("\0{Ͽ⌜ƀ")
You just need to get the first two bytes of the Data as UInt16.
var result: UInt16 = 0
_ = withUnsafeMutableBytes(of: &result) {characteristic.value!.copyBytes(to: $0, from: 0...1)}
print("\(result) degrees")
Assuming your format of uint16 is in Little-Endian as well as iOS, which is more often found in BLE characteristics.
(When you get the first value in Data, from: 0...1 is not needed, but you may want some data in other position.)

Optional UInt8? gives 2 byte memory size

UInt8 memory size is 1 byte . but when i make it optional value, it gives 2 byte of size.
var serail : UInt8? = 255
print(MemoryLayout.size(ofValue: serail)) // it gives 2 byte size.
var serail : UInt8 = 255
print(MemoryLayout.size(ofValue: serail)) // it gives 1 byte size.
how to get exactly 1 byte memory size for Integer value
Under the hood, an optional is an enum and looks something like this:
enum Optional<Wrapped> {
case some(Wrapped) // not nil
case none // nil
}
The symbols ? and ! are just shorthands for referring to this optional enum. This extra layer causes it to be 2 bytes large.
However, if you unwrap the optional, the returned value is the wrapped value itself, so it becomes 1 byte.

How to use Int value with specific byte length

Need an Integer property 3 bytes length.
The main goal is I have structure:
packet {
header: UInt8
parameters: 3 bytes
reserve: UInt16
}
Which have to be converted to Data 6 bytes in total!
Guys pls help how can I achive this?

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
}