How to convert a 4-bytes array into the corresponding Int?
let array: [UInt8] ==> let value : Int
Example:
Updated for Swift 5, two things to pay attention:
As [UInt8] is stored in a contiguous region of memory, there's no need to convert it to Data, pointer can access all bytes directly.
Int's byte order is little endian currently on all Apple platform, but this is not garanteed on other platforms.
say we want [0, 0, 0, 0x0e] to convert to 14. (big-endian byte order)
let source: [UInt8] = [0, 0, 0, 0x0e]
let bigEndianUInt32 = source.withUnsafeBytes { $0.load(as: UInt32.self) }
let value = CFByteOrderGetCurrent() == CFByteOrder(CFByteOrderLittleEndian.rawValue)
? UInt32(bigEndian: bigEndianUInt32)
: bigEndianUInt32
print(value) // 14