加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

将String转换为int8数组

发布时间:2020-12-14 04:45:18 所属栏目:百科 来源:网络整理
导读:我有一个C结构(旧库,blah blah blah),它包含一个C字符串,现在我需要将CFString和 Swift字符串转换为这个c字符串.就像是 struct Product{ char name[50]; char code[20];} 所以我试着把它指定为 productName.getCString(myVarOfStructProduct.name,maxLength:
我有一个C结构(旧库,blah blah blah),它包含一个C字符串,现在我需要将CFString和 Swift字符串转换为这个c字符串.就像是

struct Product{
   char name[50];
   char code[20];
}

所以我试着把它指定为

productName.getCString(&myVarOfStructProduct.name,maxLength: 50,encoding: NSUTF8StringEncoding)

但是编译器给出了以下错误:无法将类型(int8,int8,int8 ….)转换为[CChar].

解决方法

可能的解决方案:

withUnsafeMutablePointer(&myVarOfStructProduct.name) {
    strlcpy(UnsafeMutablePointer($0),productName,UInt(sizeofValue(myVarOfStructProduct.name)))
}

在块内,$0是指向元组的(可变)指针.这个指针是
转换为UnsafeMutablePointer< Int8>正如预期的那样
BSD library function strlcpy().

它还使用了Swift字符串productName自动生成的事实
到UnsafePointer< UInt8>
正如在String value to UnsafePointer<UInt8> function parameter behavior中所解释的那样.正如在评论中所提到的那样
线程,这是通过创建一个临时的UInt8数组(或序列?)来完成的.
因此,您可以显式枚举UTF-8字节并放置它们
进入目的地:

withUnsafeMutablePointer(&myVarOfStructProduct.name) {
    tuplePtr -> Void in
    var uint8Ptr = UnsafeMutablePointer<UInt8>(tuplePtr)
    let size = sizeofValue(myVarOfStructProduct.name)
    var idx = 0
    if size == 0 { return } // C array has zero length.
    for u in productName.utf8 {
        if idx == size - 1 { break }
        uint8Ptr[idx++] = u
    }
    uint8Ptr[idx] = 0 // NUL-terminate the C string in the array.
}

另一种可能的解决方案(使用中间NSData对象):

withUnsafeMutablePointer(&myVarOfStructProduct.name) {
    tuplePtr -> Void in
    let tmp = productName + String(UnicodeScalar(0)) // Add NUL-termination
    let data = tmp.dataUsingEncoding(NSUTF8StringEncoding,allowLossyConversion: true)!
    data.getBytes(tuplePtr,length: sizeofValue(myVarOfStructProduct.name))
}

Swift 3更新:

withUnsafeMutablePointer(to: &myVarOfStructProduct.name) {
    $0.withMemoryRebound(to: Int8.self,capacity: MemoryLayout.size(ofValue: myVarOfStructProduct.name)) {
        _ = strlcpy($0,MemoryLayout.size(ofValue: myVarOfStructProduct.name))
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读