将Int [int8]的带符号数组快速转换为Int [UInt8]的无符号数组 [英] Swift converting signed array of Int [int8] to unsigned array of Int [UInt8]

查看:1338
本文介绍了将Int [int8]的带符号数组快速转换为Int [UInt8]的无符号数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将[Int8]的有符号数组转换为[UInt8]的无符号数组。

How to convert signed array of [Int8] to unsigned array of [UInt8].

 let arryData: [Int8] =  [-108, 11, -107, -14, 35, -57, -116, 118, 54, 91, 12, 67, 21, 29, -44, 111]

我只想将上面的内容转换为无符号[UInt8]数组。如何在迅捷中实现这一目标。在此先感谢。

I just want to convert this above into array of Unsigned [UInt8]. How to achieve this in swift.? Thanks in advance.

推荐答案

如果您打算将带符号的8位整数转换为
无符号的具有相同的位表示(例如 -1 - > 255 ):

If your intention is to convert signed 8-bit integers to unsigned ones with the same bit representation (e.g. -1 -> 255):

let intArray: [Int8] =  [0, 1, 2, 127, -1, -2, -128]
let uintArray = intArray.map { UInt8(bitPattern: $0) }

print(uintArray)
// [0, 1, 2, 127, 255, 254, 128]

这篇关于将Int [int8]的带符号数组快速转换为Int [UInt8]的无符号数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆