简体   繁体   English

Swift从UnsafeMutablePointer获取数据<Int8>

[英]Swift get data from UnsafeMutablePointer<Int8>

I have a objc function like below 我有一个如下的objc函数

-(char *)decrypt:(char *)crypt el:(int)el{}

when I call this function from swift it returns UnsafeMutablePointer<Int8> . 当我迅速调用此函数时,它返回UnsafeMutablePointer<Int8> Now I need to get the data from this pointer. 现在,我需要从该指针获取数据。 The output should be a string like this 输出应该是这样的字符串

"5c9f2cb88787fff26ca8a57982604460201805111017510011111111" “ 5c9f2cb88787fff26ca8a57982604460201805111017510011111111”

I have tried below code to retrieve the value 我尝试下面的代码来检索值

String(validatingUTF8: pointer)

But it returns nil. 但是它返回nil。 How do I get the value from this UnsafeMutablePointer<Int8>? 如何从此UnsafeMutablePointer<Int8>?获取值UnsafeMutablePointer<Int8>?

Best guess is that the data is no a valid UTF-8 string. 最好的猜测是该数据不是有效的UTF-8字符串。

If there decryption failed the result is more than likely invalid UTF-8 but this is just a diagnostic. 如果解密失败,则结果很可能是无效的UTF-8,但这只是诊断。

Look at the hex representation and verify it is valid or use String(validatingUTF8: pointer) where non UTF-8 values will be displayed as the unicode error symbol: . 查看十六进制表示形式并确认其有效或使用String(validatingUTF8: pointer) ,其中非UTF-8值将显示为Unicode错误符号:。

I'm going to go out on a limb here and assume that your char * doesn't actually point to UTF-8 data, but rather to raw binary data of which you are hoping to get a hexadecimal representation. 我将在这里走出去,假设您的char *实际上并不指向UTF-8数据,而是指向您希望获取其十六进制表示形式的原始二进制数据。 You can do that via: 您可以通过以下方式实现:

// You'll need to know the length of the data. If it's null-terminated,
// read through the pointer until you hit a zero byte; otherwise, use whatever
// facility the API you're using provides to get the data length.
let buffer = UnsafeBufferPointer(start: $0, count: __insert_length_here__)

// and so:
let hexString = buffer.reduce(into: "") { $0 += String(format: "%02x", $1) }

print(hexString) // ta da!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift 将数据转换为 UnsafeMutablePointer<Int8> - Swift convert Data to UnsafeMutablePointer<Int8> iOS:转换UnsafeMutablePointer <Int8> swift中的字符串? - iOS: Convert UnsafeMutablePointer<Int8> to String in swift? 如何快速将 Int8 转换为 Uint8? - how to convert Int8 to Uint8 in swift? 如何使用Swift的UnsafeMutablePointer <UnsafeMutablePointer <Void >>引用? - How do I work with a UnsafeMutablePointer<UnsafeMutablePointer<Void>> reference from Swift? 无法使用类型为&#39;(UInt32,UnsafePointer的参数列表&#39;调用&#39;CCHmac&#39; <Int8> ,UInt,[CChar],Int,UnsafeMutablePointer <CUnsignedChar> ) - Cannot invoke 'CCHmac' with an argument list of type '(UInt32, UnsafePointer<Int8>, UInt, [CChar], Int, UnsafeMutablePointer<CUnsignedChar>) 如何dealloc从Swift结构中引用的UnsafeMutablePointer - How to dealloc UnsafeMutablePointer referenced from Swift struct swift 3 iOS:将UInt8数组转换为int8 - swift 3 ios : convert UInt8 Array to int8 将Int [int8]的带符号数组快速转换为Int [UInt8]的无符号数组 - Swift converting signed array of Int [int8] to unsigned array of Int [UInt8] 如何获取 UnsafeMutablePointer 的长度<Object>在 Swift 中写入 OpenSSL BIO? - How to get length of a UnsafeMutablePointer<Object> written to a OpenSSL BIO in Swift? 如何将 `Int8?` 转换为 `Int?`? - How to cast `Int8?` to `Int?`?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM