Anthony Shahine
Anthony Shahine

Reputation: 2487

swift 3 ios : convert UInt8 Array to int8

i have an UInt8 array that contains values between 0 and 255

i would like to convert this array to Int8 array in order to contains values between -128 and 127

How to achieve this in swift?

Upvotes: 1

Views: 4586

Answers (2)

vadian
vadian

Reputation: 285250

Use map and the appropriate initialiser:

let intArray = uintArray.map { Int8(bitPattern: $0) }

Upvotes: 8

bscothern
bscothern

Reputation: 2014

You can convert a [UInt8] to a [Int8] by using this:

for el in uint8Array {
    uint8Array.append(UInt8(bitPattern: el))
}

Upvotes: 5

Related Questions