Swift: How to convert a String to UInt8 array? -
how convert string uint8 array?
var str = "test" var ar : [uint8] ar = str
lots of different ways, depending on how want handle non-ascii characters.
but simplest code use utf8
view:
let string = "hello" let array: [uint8] = array(string.utf8)
note, result in multi-byte characters being represented multiple entries in array, i.e.:
let string = "é" print(array(string.utf8))
prints out [195, 169]
there’s .nulterminatedutf8
, same thing, adds nul-character end if plan pass somewhere c string (though if you’re doing that, can use .withcstring
or use implicit conversion bridged c functions.
Comments
Post a Comment