我是 kotlin 多平臺的新手,我想將此函式從目標 C 轉換為 kotlin
NSString *RCTMD5Hash(NSString *string)
{
const char *str = string.UTF8String;
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(str, (CC_LONG)strlen(str), result);
return [NSString stringWithFormat:@"xxxxxxxxxxxxxxxx",
result[0],
result[1],
result[2],
result[3],
result[4],
result[5],
result[6],
result[7],
result[8],
result[9],
result[10],
result[11],
result[12],
result[13],
result[14],
result[15]];
}
我的科特林代碼:
fun md5Hash(key: NSString): String {
val str = key.UTF8String
val result = alloc<UByteVar>()
CC_MD5(str, strlen(str.toString()).toCC_LONG(), result)
return NSString.stringWithFormat("xxxxxxxxxxxxxxxx", result[0],
result[1],
result[2],
result[3],
result[4],
result[5],
result[6],
result[7],
result[8],
result[9],
result[10],
result[11],
result[12],
result[13],
result[14],
result[15])
}
我有一些錯誤無法解決

uj5u.com熱心網友回復:
在 Kotlin 中分配 C 記憶體,memScoped應該使用。
在您的情況下,您需要分配一個陣列,這就是為什么allocArray應該使用而不是 a alloc:
fun md5Hash(key: NSString): String {
memScoped {
val str = key.UTF8String
val result = allocArray<UByteVar>(CC_MD5_DIGEST_LENGTH)
CC_MD5(str, strlen(key.toString()).toUInt(), result)
return NSString.stringWithFormat(
"xxxxxxxxxxxxxxxx",
result[0],
result[1],
result[2],
result[3],
result[4],
result[5],
result[6],
result[7],
result[8],
result[9],
result[10],
result[11],
result[12],
result[13],
result[14],
result[15]
)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/453650.html
標籤:安卓 IOS 目标-c 科特林 kotlin-多平台
