在Swift中将bytes / Uint8数组转换为Int

时间:2015-09-24 20:01:44

标签: arrays swift int byte

如何将4字节数组转换为相应的Int?

let array: [UInt8] ==> let value : Int

示例:

输入:

\0\0\0\x0e

输出:

14

我在互联网上发现的一些代码无效:

let data = NSData(bytes: array, length: 4)
data.getBytes(&size, length: 4)
// the output to size is 184549376

7 个答案:

答案 0 :(得分:33)

有两个问题:

  • Int是64位平台上的64位整数,即输入数据 只有32位。
  • Int在所有当前的Swift平台上使用little-endian表示, 你的输入是big-endian。

据说以下内容可行:

let array : [UInt8] = [0, 0, 0, 0x0E]
var value : UInt32 = 0
let data = NSData(bytes: array, length: 4)
data.getBytes(&value, length: 4)
value = UInt32(bigEndian: value)

print(value) // 14

或者在Swift 3中使用Data

let array : [UInt8] = [0, 0, 0, 0x0E]
let data = Data(bytes: array)
let value = UInt32(bigEndian: data.withUnsafeBytes { $0.pointee })

使用一些缓冲区指针魔术你可以避免中间 复制到NSData对象(Swift 2):

let array : [UInt8] = [0, 0, 0, 0x0E]
var value = array.withUnsafeBufferPointer({ 
     UnsafePointer<UInt32>($0.baseAddress).memory
})
value = UInt32(bigEndian: value)

print(value) // 14

对于此方法的Swift 3版本,请参阅环境光的答案。

答案 1 :(得分:14)

在Swift 3中,它现在更加冗长:

let array : [UInt8] = [0, 0, 0, 0x0E]
let bigEndianValue = array.withUnsafeBufferPointer {
         ($0.baseAddress!.withMemoryRebound(to: UInt32.self, capacity: 1) { $0 })
}.pointee
let value = UInt32(bigEndian: bigEndianValue)

答案 2 :(得分:7)

我认为马丁的回答比这更好,但我仍想发布我的回答。任何建议都会非常有用。

let array : [UInt8] = [0, 0, 0, 0x0E]
var value : Int = 0
for byte in array {
    value = value << 8
    value = value | Int(byte)
}
print(value) // 14

答案 3 :(得分:3)

这里有一些很好的答案,非常高兴看到^^ 但是,如果您想避免与Swift的C-interopability API进行交互,那么我建议您查看我的示例。对于所有数据类型大小,它也是通用的。请注意,MemoryLayout仅用于健全性检查。

代码:

public extension UnsignedInteger {
    init(_ bytes: [UInt8]) {
        precondition(bytes.count <= MemoryLayout<Self>.size)

        var value: UInt64 = 0

        for byte in bytes {
            value <<= 8
            value |= UInt64(byte)
        }

        self.init(value)
    }
}

使用示例:

let someBytes = [UInt8](repeating: 0x42, count: 2)
let someValue = UInt16(someBytes)

对于小端支持,您需要for byte in bytes.reversed()

答案 4 :(得分:0)

对于那些喜欢用老式方式做的人,这里有一组从字节数组中获取int值的方法。这适用于正在按顺序处理包含各种数据的字节数组的情况。

/// Class which encapsulates a Swift byte array (an Array object with elements of type UInt8) and an
/// index into the array.
open class ByteArrayAndIndex {

   private var _byteArray : [UInt8]
   private var _arrayIndex = 0

   public init(_ byteArray : [UInt8]) {
      _byteArray = byteArray;
   }

   /// Property to provide read-only access to the current array index value.
   public var arrayIndex : Int {
      get { return _arrayIndex }
   }

   /// Property to calculate how many bytes are left in the byte array, i.e., from the index point
   /// to the end of the byte array.
   public var bytesLeft : Int {
      get { return _byteArray.count - _arrayIndex }
   }

   /// Method to get a single byte from the byte array.
   public func getUInt8() -> UInt8 {
      let returnValue = _byteArray[_arrayIndex]
      _arrayIndex += 1
      return returnValue
   }

   /// Method to get an Int16 from two bytes in the byte array (little-endian).
   public func getInt16() -> Int16 {
      return Int16(bitPattern: getUInt16())
   }

   /// Method to get a UInt16 from two bytes in the byte array (little-endian).
   public func getUInt16() -> UInt16 {
      let returnValue = UInt16(_byteArray[_arrayIndex]) |
                        UInt16(_byteArray[_arrayIndex + 1]) << 8
      _arrayIndex += 2
      return returnValue
   }

   /// Method to get a UInt from three bytes in the byte array (little-endian).
   public func getUInt24() -> UInt {
      let returnValue = UInt(_byteArray[_arrayIndex]) |
                        UInt(_byteArray[_arrayIndex + 1]) << 8 |
                        UInt(_byteArray[_arrayIndex + 2]) << 16
      _arrayIndex += 3
      return returnValue
   }

   /// Method to get an Int32 from four bytes in the byte array (little-endian).
   public func getInt32() -> Int32 {
      return Int32(bitPattern: getUInt32())
   }

   /// Method to get a UInt32 from four bytes in the byte array (little-endian).
   public func getUInt32() -> UInt32 {
      let returnValue = UInt32(_byteArray[_arrayIndex]) |
                        UInt32(_byteArray[_arrayIndex + 1]) << 8 |
                        UInt32(_byteArray[_arrayIndex + 2]) << 16 |
                        UInt32(_byteArray[_arrayIndex + 3]) << 24
      _arrayIndex += 4
      return returnValue
   }

   /// Method to get an Int64 from eight bytes in the byte array (little-endian).
   public func getInt64() -> Int64 {
      return Int64(bitPattern: getUInt64())
   }

   /// Method to get a UInt64 from eight bytes in the byte array (little-endian).
   public func getUInt64() -> UInt64 {
      let returnValue = UInt64(_byteArray[_arrayIndex]) |
                        UInt64(_byteArray[_arrayIndex + 1]) << 8 |
                        UInt64(_byteArray[_arrayIndex + 2]) << 16 |
                        UInt64(_byteArray[_arrayIndex + 3]) << 24 |
                        UInt64(_byteArray[_arrayIndex + 4]) << 32 |
                        UInt64(_byteArray[_arrayIndex + 5]) << 40 |
                        UInt64(_byteArray[_arrayIndex + 6]) << 48 |
                        UInt64(_byteArray[_arrayIndex + 7]) << 56
      _arrayIndex += 8
      return returnValue
   }
}

这是一个较大类的摘录,包括提取字符串和其他类型数据的方法。另见:https://stackoverflow.com/a/41592206/253938

答案 5 :(得分:0)

当您不知道字节数组的大小(或Data的大小)时,就会出现可接受的答案的问题

它与let array : [UInt8] = [0, 0, 0x23, 0xFF]

一起使用时效果很好

但是它不适用于let array : [UInt8] = [0x23, 0xFF]
(因为它将被视为[0x23, 0xFF, 0, 0]

这就是为什么我喜欢按位操作的@Jerry饼干。

我已经制作了他的代码段的功能版本。

let data = Data(bytes: [0x23, 0xFF])
let decimalValue = data.reduce(0) { v, byte in
    return v << 8 | Int(byte)
}

答案 6 :(得分:0)

最有效,最安全的方法应该是按位运算符。
这是它的通用扩展名:

enum Endianness {
    case big, little
}

extension UnsignedInteger {
    init(
        _ bytes: [UInt8],
        endianness: Endianness = .big
    ) {
        let data = endianness == .big ? bytes : bytes.reversed()
        self = data.reduce(0) { acc, val in
            return acc << 8 | Self(val)
        }
    }
}

使用它:

// 153
UInt([0x00, 0x99])
// 39168
UInt([0x00, 0x99], endianness: .little)
相关问题