Reputation: 1144
In Java we have data type called byte
but in Swift there is no byte
data type available.
In the example below I typecasted the ASCII (0xAF = 175) value by using byte
data type.
byte abyte = (byte) 0xAF;
And the result is abyte= -89
How do I acheive this in Swift?
Upvotes: 3
Views: 11152
Reputation: 285
Swift bytes is [UInt8], UInt8 is 0 ... 255, Int8 is -128 ~ 127, you can get the Int8 value which like java byte with follow code.
extension UInt8 {
var int8: Int8 {
return Int8(self > 127 ? Int(self) - 256 : Int(self))
}
}
Upvotes: 5
Reputation: 1129
The data type is Unsigned Int 8 (UInt8).
var byte:UInt8 = 0xAF
For a string of bytes:
var bytes:[UInt8] = [0xAF,0xAB]
For the bytes from data:
var data = Data()
var bytes = data.bytes //[UInt8]
Upvotes: 13