简体   繁体   English

快速将字节数组转换为整数

[英]Swift converting a byte array to integers

Hello I am new to swift and would like to convert a byte array to several integers. 您好,我是swift的新手,并希望将字节数组转换为几个整数。 I have written working code in Java but I am not quite sure how to take it to swift 我已经用Java编写了工作代码,但是我不太确定如何迅速使用它

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: 到目前为止,这是我在Swift中所拥有的:

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. 该函数将其带到一个字节数组,但是我不确定要使用哪个Swift类来模仿Java中DataInputStream的行为。

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 ) 非常感谢Martin R向我展示了他上面链接的帖子( 解析swift(3)数据流的惯用方法

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 } 我基本上从Martin R链接的帖子中获取了该类,并将其修改为使用NSData对象而不是Data对象:class DataStream {let data:NSData var index = 0 var atEnd:Bool {返回索引> = 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(): 使用此DataStream类以及几个NSData方法,我可以模仿Java方法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. 我是Swift的新手,所以这可能不是最好的方法。 This works with Swift 3.1. 这适用于Swift 3.1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM