cswiftunsafemutablepointerbridging

UnsafeMutablePointer<Int8> to UTF8 String


I have a UnsafeMutablePointer pointing at a C string, I want to convert the bytes at this pointer to a Swift String, the data is a C string, so UTF8.

Is it possible to iterate byte by byte until NULL? Or some easier way?


Solution

  • CChar is an alias for Int8, so you can use CChar-based methods here.

    let str = String(cString: pointer)