Reputation: 1094
I have not been able to progress past the point illustrated below.
I am getting:
Cannot convert value of type 'Element' to expected argument type 'IntegerLiteralType' (aka 'Int')
on the line where I am moving data from 'Element' to 'T'. Also, I have not yet figured out how to extended truncating to UInt.
Any assistance will be greatly appreciated.
protocol Truncating {
init(truncatingBitPattern: IntegerLiteralType)
}
extension Int {
init(truncatingBitPattern value: Int) { self.init(truncatingBitPattern: value.toIntMax()) }
}
protocol Bitshiftable {
func <<(lhs: Self, rhs: Self) -> Self
func >>(lhs: Self, rhs: Self) -> Self
func <<=(inout lhs: Self, rhs: Self)
func >>=(inout lhs: Self, rhs: Self)
}
protocol ArrayConvertable: IntegerType, Bitshiftable, Truncating {}
extension Int : ArrayConvertable {}
extension Int8 : ArrayConvertable {}
extension Int16 : ArrayConvertable {}
extension Int32 : ArrayConvertable {}
extension Int64 : ArrayConvertable {}
//extension UInt : ArrayConvertable {}
extension UInt8 : ArrayConvertable {}
extension UInt16 : ArrayConvertable {}
extension UInt32 : ArrayConvertable {}
extension UInt64 : ArrayConvertable {}
extension Array where Element: ArrayConvertable {
func toInt<T: ArrayConvertable>() -> T?
{
let targetSize = sizeof(T)
let sourceSize = sizeof(Element)
let sourceByteCount = count * sourceSize
guard targetSize == sourceByteCount else { return nil }
var n: T = 0
for e in self {
var dataChunk = e
for i in 0..<sourceSize {
n = n | T(truncatingBitPattern: dataChunk)
if i + 1 < sourceSize {
n = n << 8
dataChunk = dataChunk >> 8
}
}
}
return n
}
}
func test() {
let a8: [UInt8] = [0xab, 0xba, 0xda, 0xba, 0xd0, 0xd1, 0xd2, 0xd3, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10]
let a16: [UInt16] = [0xabba, 0xdaba, 0xd0d1, 0xd2d3, 0xfedc, 0xba98, 0x7654, 0x3210]
let a32: [UInt32] = [0xabbadaba, 0xd0d1d2d3, 0xfedcba98, 0x76543210]
let a64: [UInt64] = [0xabbadabad0d1d2d3, 0xfedcba9876543210]
var n8: UInt8 = 0
var n16: UInt16 = 0
var n32: UInt32 = 0
var n64: UInt64 = 0
print("--- 08 bit source")
n8 = a8.toInt()!
n16 = a8.toInt()!
n32 = a8.toInt()!
n64 = a8.toInt()!
print(n8, n16, n32, n64)
print("--- 16 bit source")
n8 = a16.toInt()!
n16 = a16.toInt()!
n32 = a16.toInt()!
n64 = a16.toInt()!
print(n8, n16, n32, n64)
print("--- 32 bit source")
n8 = a32.toInt()!
n16 = a32.toInt()!
n32 = a32.toInt()!
n64 = a32.toInt()!
print(n8, n16, n32, n64)
print("--- 64 bit source")
n8 = a64.toInt()!
n16 = a64.toInt()!
n32 = a64.toInt()!
n64 = a64.toInt()!
print(n8, n16, n32, n64)
}
test()
Upvotes: 0
Views: 111
Reputation: 539965
Here is s possible solution which just re-interprets the data. No protocol is needed:
extension Array where Element: IntegerType {
func toInt<T: IntegerType>() -> T? {
guard sizeof(T) <= count * sizeof(Element) else { return nil }
return UnsafePointer<T>(self).memory
}
}
Example:
let a32: [UInt32] = [0x11223344, 0x55667788, 0x99AABBCC, 0xDDEEFF00]
if let u16: UInt16 = a32.toInt() {
print(String(format:"%#04x", u16)) // 0x3344
}
Upvotes: 1