Convert CUnsignedChar pointer to CChar pointer

88 views Asked by At

I have an objective C function that gives me a

UnsafePointer<UInt8>/UnsafePointer<CUnsignedChar>. 

However I need this data as a

UnsafePointer<Int8>

unfortunately the function I require doesn't take

UnsafePointer<UInt8>.
1

There are 1 answers

2
Martin R On BEST ANSWER

In your case an "unsafe bit cast" would be the easiest solution:

let ptr: UnsafePointer<UInt8> = ...
let i8ptr = unsafeBitCast(ptr, to: UnsafePointer<Int8>.self)

Another option is "memory rebinding":

ptr.withMemoryRebound(to: Int8.self, capacity: <count>) { i8ptr in
    // ...
}