Sto cercando un modo diretto per eseguire il cast dei bit dei valori di Int a UInt e viceversa. Per esempio (utilizzando gli 8 bit interi per semplicità) che voglio raggiungere il seguente:Int to UInt (e viceversa) bit casting in Swift
let unsigned: UInt8 = toUInt8(-1) // unsigned is 255 or 0xff
let signed: Int8 = toInt8(0xff) // signed is -1
In un primo momento mi è venuto fuori con la seguente soluzione:
let unsigned = unsafeBitCast(Int8(-1), UInt8.self)
let signed = unsafeBitCast(UInt8(0xff), Int8.self)
Ma Apple nel "unsafeBitCast() "la documentazione afferma quanto segue:
.. Attenzione :: Rompe le garanzie del sistema di tipo Swift; utilizzare con estrema cura. C'è quasi sempre un modo migliore per fare qualsiasi cosa.
Qualcuno ha la via migliore?
Domanda simile qui: [Conversione firmata a unsigned in Swift] (http://stackoverflow.com/questions/25666846/converting-signed-to-unsigned-in-swift). –
@ Martin sì, hai ragione. Giuro che ho cercato questo in google e stackoveflow, non è mai apparso. –