Error[8]: Undefined offset: 7, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我有一个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(strlcpy()),productname,UInt(sizeofValue(myVarOfStructProduct.name)))}

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

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.}
.

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

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))}

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

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

Swift 3更新:

[+++] 总结

以上是内存溢出为你收集整理的将String转换为int8数组全部内容,希望文章能够帮你解决将String转换为int8数组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 8, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我有一个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(strlcpy()),productname,UInt(sizeofValue(myVarOfStructProduct.name)))}

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

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.}
.

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

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))}

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

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

Swift 3更新:

[+++] 总结

以上是内存溢出为你收集整理的将String转换为int8数组全部内容,希望文章能够帮你解决将String转换为int8数组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 9, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我有一个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(strlcpy()),productname,UInt(sizeofValue(myVarOfStructProduct.name)))}

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

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.}
.

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

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))}

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

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

Swift 3更新:

[+++] 总结

以上是内存溢出为你收集整理的将String转换为int8数组全部内容,希望文章能够帮你解决将String转换为int8数组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
将String转换为int8数组_app_内存溢出

将String转换为int8数组

将String转换为int8数组,第1张

概述我有一个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(strlcpy()),productname,UInt(sizeofValue(myVarOfStructProduct.name)))}

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

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.}
.

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

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))}

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

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

Swift 3更新:

总结

以上是内存溢出为你收集整理的将String转换为int8数组全部内容,希望文章能够帮你解决将String转换为int8数组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1008549.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存