Convert bytes/UInt8 array to Int in Swift

前端 未结 7 575
野性不改
野性不改 2020-12-01 03:56

How to convert a 4-bytes array into the corresponding Int?

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

Example:

Input:

         


        
7条回答
  •  臣服心动
    2020-12-01 04:18

    For those who prefer to do it the old-fashioned way, here's a set of methods for getting int values from a byte array. This is intended for situations where a byte array containing various kinds of data is being processed sequentially.

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

    This is an extract from a larger class that includes methods for extracting strings and other kinds of data. See also here: https://stackoverflow.com/a/41592206/253938

提交回复
热议问题