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

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -