首页 > 编程知识 正文

c语言字符串转换成数组,c语言实现将字符串转换成数字

时间:2023-05-04 13:29:51 阅读:272805 作者:3947

C数组char name [8]作为元组导入Swift:

(Int8, Int8, Int8, Int8, Int8, Int8, Int8, Int8)

名称的地址与名称[0]的地址相同,和

Swift保留从C导入的结构的内存布局

confirmed by Apple engineer 眼睛大的哈密瓜 Groff:

… You can leave the struct defined in C and import it into Swift. Swift will respect C’s layout.

因此,我们可以传递record.name的地址,

转换成一个UInt8指针

String初始化器:

var record = someFunctionReturningAStructRecord()

// Swift 2:

let name = withUnsafePointer(&record.name) {

String.fromCString(UnsafePointer($0))!

}

// Swift 3:

let name = withUnsafePointer(to: &record.name) {

$0.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout.size(ofValue: record.name)) {

String(cString: $0)

}

}

注意:假定名称[]中的字节是有效的NUL终止的UTF-8序列。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。