'How to convert Data/NSData to an Array of Integers using Swift

I have a Data that I received from a characteristic in a Service

let value = characteristic.value

This "value" of type Data .

In this value there are 20 bytes that contains 5 numbers of type Uint8 or int 1,2,3,4,5.

How do I get these Integer numbers from this Data value???



Solution 1:[1]

To convert Data/NSData to an array of Integer

// here value is Data type which contain array of byte
let value = characteristic.value

let B0 = value[0]
let B1 = value[1]
let B2 = value[2]
let B3 = value[3]
let B4 = value[4]
let B5 = value[5]

// create a byte of array
let generationByteData: [UInt8] = [B0, B1, B2, B3, B4, B5]
print("generationByteData = \(generationByteData)")

// Result = generationByteData = [3, 17, 34, 0, 0, 0]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yogesh Rathore