Swift 2.1 [UInt8]

编程入门 行业动态 更新时间:2024-10-24 20:14:54
本文介绍了Swift 2.1 [UInt8] --utf8-->细绳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Stack Overflow 和其他地方都存在这样的问题.但它似乎也进化了很多.

I know questions like this exist on both Stack Overflow and elsewhere. But it seems to have evolved a lot as well.

给定一个 UInt8 列表(基本上是一个 swift 字节数组),将它转换为 swift String 的最简单/惯用的方法是什么?

Given a list of UInt8 (a swift byte array basically), what is the easiest/idiomatic way to covert it to a swift String?

我对不使用 NSData/NSString 的方法特别感兴趣,因为如果圣诞老人将 Swift 带到 Linux 世界,它无疑将没有 NS 库,我想知道该怎么做它只是在 Swift 中.

I'm particularly interested in the method that doesn't use NSData/NSString, since if Santa brings Swift to the world of Linux, it will undoubtedly be without the NS libraries, and I'd like to know how to do it in just Swift.

推荐答案

let buffUInt8: Array<UInt8> = [97, 98, 115, 100, 114, 102, 103, 104, 0]

// you need Int8 array
let buffInt8 = buffUInt8.map{ Int8(bitPattern: $0)}
let str = String.fromCString(buffInt8) // "absdrfgh"

或者你可以使用

String.fromCStringRepairingIllFormedUTF8(cs: UnsafePointer<CChar>) -> (String?, hadError: Bool)

这篇关于Swift 2.1 [UInt8] --utf8-->细绳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-30 11:20:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/777192.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!