【问题标题】:What is the right equivalent for Java array of bytes in Swift?Swift 中 Java 字节数组的正确等价物是什么?
【发布时间】:2018-02-18 12:12:07
【问题描述】:

我才刚刚开始涉足 Swift 开发。我在Java中有以下方法:

  public static byte[] addChecksum(byte[]command, boolean isDeviceSendFormat) {
    int checksum = 0;
    int l = command.length;
    for (int i=0; i<l-2; i++) {

        if (i==1 && isDeviceSendFormat==true) {
            continue;
        }

        int val = command[i];
        if (val < 0) {
            val = 0x100 + val;
        }
        checksum += val;
    }

    if (l > 2) {
        if (isDeviceSendFormat == false) {
            command[l - 1] = (byte) (checksum % 0x100);  // LSB
            command[l - 2] = (byte) (checksum / 0x100);  // MSB
        }
        else {
            command[l - 2] = (byte) (checksum % 0x100);  // LSB
            command[l - 1] = (byte) (checksum / 0x100);  // MSB
        }
    }

    return command;
}

我需要翻译成 Swift,但我遇到了一些问题,这是我目前得到的:

func addCheckSum(bufferInput:[UInt8], isDeviceSendFormat: Bool) -> [UInt8]{
    var checksum: UInt8 = 0
    var length: Int = 0
    var iIndex: Int
    var bufferOutput: [UInt8]

    length = bufferInput.count        

    for (index, value) in bufferInput.enumerated() {
        if index < bufferInput.count - 2 {
            if value == 1 && isDeviceSendFormat {
                continue
            }

            var val:UInt8 = bufferInput[index]
            if (val < 0) {
                val = 0x100 + val //Error line
            }
            checksum = checksum + val
        }
    }
}

但我收到以下错误:Integer literal '256' overflows when stored into 'UInt8' 在上面代码的注释行中。如何将此方法从 Java 转换为 Swift?

【问题讨论】:

标签: java swift byte checksum


【解决方案1】:

这是我从你的 Java 代码到 Swift 的翻译:

public static func addChecksum(_ command: inout [UInt8], isDeviceSendFormat: Bool) -> [UInt8] {
    var checksum: UInt32 = 0
    let l: Int = command.count
    for i in 0..<l-2 {

        if i == 1 && isDeviceSendFormat {
            continue
        }

        let val = UInt32(command[i])
        //No need to modify `val` as it takes non-negative value when `command` is `[UInt8]`.
        checksum += val
    }

    if l > 2 {
        if !isDeviceSendFormat {
            command[l - 1] = UInt8(checksum % 0x100)  // LSB
            command[l - 2] = UInt8(truncatingIfNeeded: checksum / 0x100)  // Next to LSB
        } else {
            command[l - 2] = UInt8(checksum % 0x100)  // LSB
            command[l - 1] = UInt8(truncatingIfNeeded: checksum / 0x100)  // Next to LSB
        }
    }

    return command
}

//Assuming `command` is not too long as to make integer overflow in `checksum += val`.

一些注意事项:

  • 这 3 行 Java 代码:

    if (val < 0) {
        val = 0x100 + val;
    }
    

    val 为负数时,通过添加0x100(=256) 将值-128...127 转换为0...255。所以,val 取 0...255 中的任何值,所以,我选择 [UInt8] 代替 command。当你选择UInt8时,上面Java中的3行在Swift中是不需要的。

  • 1234563假设整数溢出可能永远不会发生,它们只采用非负值,因此非负 32 位长整数将是合适的。

在 Swift 中没有直接等同于 Java 的 byte[]。所以,在某些情况下[UInt8][Int8] 更合适。而且你可以发现很多情况下Java的byte[]被翻译成Swift的Data

【讨论】:

  • 首先非常感谢您的详细解释。为您的努力添加了 +1。我现在正在尝试在我的项目中实施它来回答你的问题。很快就会给出反馈。
  • 再次感谢您的帮助,您帮了我很多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-30
  • 1970-01-01
  • 2014-09-24
  • 2014-07-31
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
相关资源
最近更新 更多