Swift converting a byte array to integers

南笙酒味 提交于 2019-12-10 10:37:58

问题


Hello I am new to swift and would like to convert a byte array to several integers. I have written working code in Java but I am not quite sure how to take it to swift

byte[] codeData = Base64.decode(codeDataBase64, 0);
    ByteArrayInputStream bais = new ByteArrayInputStream(codeData);
    DataInputStream dis = new DataInputStream(bais);

    byte version = dis.readByte();
    if (version == 1) {
        int anID = dis.readInt();
        int anotherID = dis.readInt();
        byte[] TK = new byte[512];
        int readTK = dis.read(TK);
        if (readTK == TK.length) {
            Log.e("photoConnect success", "anID=" + anID + ", anotherID=" + anotherID + ", TK.length=" + TK.length);

Here is what I have in Swift thus far:

func base64ToByteArray(base64String: String) -> [UInt8]? {
    if let nsdata = NSData(base64Encoded: base64String) {
        var bytes = [UInt8](repeating: 0, count: nsdata.length)
        nsdata.getBytes(&bytes, length: nsdata.length)
        return bytes
    }
    return nil // Invalid input
}

This function takes it to an array of bytes but I am not sure what Swift class to use to mimic the behavior of DataInputStream in Java.


回答1:


Alright, so I have a working solution. Big thanks to Martin R for showing me the post he linked above (Idiomatic method of parsing swift(3) data streams)

I basically took the class from the post Martin R linked and modified it to use an NSData object instead of a Data object: class DataStream { let data : NSData var index = 0 var atEnd : Bool { return index >= self.data.length }

init(data:NSData) {
    self.data = data
}

func next() -> UInt8? {
    guard !self.atEnd else { return nil }
    let byte = self.data.subdata(with: NSRange(location: self.index, length: 1))[0]
    self.index += 1
    return byte
}

func next(_ count:Int) -> NSData? {
    guard self.index + count <= self.data.length else { return nil }
    let subdata = self.data.subdata(with: NSRange(location: self.index, length: count))
    self.index += count
    return subdata as NSData?
}

func upTo(_ marker:UInt8) -> NSData? {
    if let end = (self.index..<self.data.length).index( where: { self.data.base64EncodedData()[$0] == marker } ) {
        let upTo = self.next(end - self.index)
        self.skip() // consume the marker
        return upTo
    }
    else {
        return nil
    }
}

func skip(_ count:Int = 1) {
    self.index += count
}

func skipThrough(_ marker:UInt8) {
    if let end = (self.index..<self.data.length).index( where: { self.data.base64EncodedData()[$0] == marker } ) {
        self.index = end + 1
    }
    else {
        self.index = self.data.length
    }
}

}

Using this DataStream class along with a couple NSData methods, I was able to mimic the behavior of the java method DataInputStream.ReadInt():

var someID : Int = 0
        //get the part of the data that represents someID
        guard let someIDData = dataStream.next(4 /*size of someID in the data*/) else { fatalError("Error getting someID bytes")}
        //convert the data bytes to an Int
        someIDData.getBytes(&someID, length: someIDData.length)

If someone has a better way to accomplish this, I would love to hear! I am new to Swift so this may not be the best way. This works with Swift 3.1.



来源:https://stackoverflow.com/questions/44549840/swift-converting-a-byte-array-to-integers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!