-3

How to convert my stringArray to int8Array. Please give me any solution to convert this. I want below type of array

let int8Array:[UInt8] = [ox55,0x55,0xff,0x01,0x0B,0x00,0x0B,0x03,0x07,0x12,0x0E,0x0C,0x10,0x09,0x12,0x0C,0x19,0x09,0xFF,0x14]

Below is my ViewController:

class ViewController:UIViewController {
var checkSum:UInt8 = 0
override func viewDidLoad() {
    super.viewDidLoad()

let stringArray:[String] = ["0x55", "0x55", "0xff", "0x01", "0x0B", "0x38", "0x18", "0x31", "0x10", "0x18", "0x0E", "0x16", "0x31", "0x10", "0x18", "0x16", "0x30", "0x11", "0x18", "0x20", "0xE1"]
    var int8Array:[UInt8] = stringArray.map{ UInt8($0.dropFirst(2), radix: 16)! }
    int8Array.removeFirst()
    int8Array.removeFirst()
    int8Array.removeFirst()
    print(int8Array)
    for item in int8Array {
        checkSum = calculateCheckSum(crc: checkSum, byteValue: UInt8(item))
    }
    print(checkSum)

}

func calculateCheckSum(crc:UInt8, byteValue: UInt8) -> UInt8 {
    let generator: UInt8 = 0x1D

    var newCrc = crc ^ byteValue

    for _ in 1...8 {
        if (newCrc & 0x80 != 0) {
            newCrc = (newCrc << 1) ^ generator
        }
        else {
            newCrc <<= 1
        }
    }
    return newCrc
}

}
Dhasal
  • 85
  • 1
  • 9

3 Answers3

0

Just map the stuff, you have to remove 0x to make the UInt8(_:radix:) initializer work.

let uint8Array = stringArray.map{ UInt8($0.dropFirst(2), radix: 16)! }
vadian
  • 274,689
  • 30
  • 353
  • 361
  • It shows me this output: [Optional(85), Optional(85), Optional(255), Optional(1), Optional(11), Optional(56), Optional(24), Optional(49), Optional(16), Optional(24), Optional(14), Optional(22), Optional(49), Optional(16), Optional(24), Optional(22), Optional(48), Optional(17), Optional(24), Optional(32), Optional(225)] – Dhasal Oct 31 '18 at 13:45
  • I want array in this format......let int8Array:[UInt8] = [ox55,0x55,0xff,0x01,0x0B,0x00,0x0B,0x03,0x07,0x12,0x0E,0x0C,0x10,0x09,0x12,0x0C,0x19,0x09,0xFF,0x14] – Dhasal Oct 31 '18 at 13:46
  • I updated the answer, you have to unwrap the optionals – vadian Oct 31 '18 at 13:47
  • i have updated my code. Please check. With using this the checksum returns is different. So I want it in [0x01,0x0B,0x00,0x0B,0x03,0x07,0x12,0x0E,0x0C,0x10,0x09,0x12,0x0C,0x19,0x09,0xFF,0x14] this format – Dhasal Oct 31 '18 at 14:04
  • The `UInt8` representation in Swift is the same as `Int`. `0x55` is the same as `85`. The `0x..` syntax can be used only for literals. – vadian Oct 31 '18 at 14:08
  • Is there any way to do this without changing our checkSum value? – Dhasal Oct 31 '18 at 14:18
  • I don't know what the checksum code does but my answer reliably maps all string hex representations to `UInt8` – vadian Oct 31 '18 at 14:20
  • When I pass this type of data to calculateCheckSum function [0x01,0x0B,0x00,0x0B,0x03,0x07,0x12,0x0E,0x0C,0x10,0x09,0x12,0x0C,0x19,0x09,0xFF,0x14] then it returns 225 and when using this [1, 11, 56, 24, 49, 16, 24, 14, 22, 49, 16, 24, 22, 48, 17, 24, 32, 225] it shows different i.e 128 – Dhasal Oct 31 '18 at 14:27
  • The arrays **are** different. – vadian Oct 31 '18 at 14:30
  • I need it same. – Dhasal Oct 31 '18 at 14:32
  • Once again if you convert a hex string array to `UInt8` using my code you will get the same. It assumes that like in the question all String values start with`0x` – vadian Oct 31 '18 at 14:35
0

First take your string array and call map on it then map it to a [UInt8] (where the total result will be [[UInt8]] and call flatMap on the result to get an array of [UInt8].. then you can do forEach on it to calculate your checksum or w/e..

[String].init().map({
    [UInt8]($0.utf8)
}).flatMap({ $0 }).forEach({
    print($0) //Print each byte or convert to hex or w/e..
})
Brandon
  • 22,723
  • 11
  • 93
  • 186
-1

If it is an option you could switch it around to specify the UInt8 array and derive the String array from that.

let int8Array: [UInt8] = [0x55, 0x55, 0xa5, 0x3f]
var stringArray: [String] {
  return int8Array.map { String(format: "0x%02X", $0) }
}

print(stringArray)
// ["0x55", "0x55", "0xA5", "0x3F"]
padarom
  • 3,529
  • 5
  • 33
  • 57