2
你好我是swift新手,想将字节数组转换为多个整数。在Java中我已经写工作代码,但我不太清楚如何把它带到迅速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);
这是我在斯威夫特迄今:
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
}
此功能需要一个数组的字节,但我不确定用什么Swift类模仿DataInputStream在Java中的行为。
@HovercraftFullOfEels请参阅我的更新问题,其中包括我的尝试和现在卡在哪里。如果我的第一个问题不够具体,我很抱歉。如果你能指出我正确的方向,我会非常感激。 – Alec
这可能会有所帮助:https://stackoverflow.com/questions/38060682/idiomatic-method-of-parsing-swift3-data-streams。 –