美文网首页
swift int 与UInt8相互转化

swift int 与UInt8相互转化

作者: 可乐小子 | 来源:发表于2021-11-10 15:06 被阅读0次

    // 将int转化为UInt8
    @objc class func toUint(signed: Int) -> UInt {

        let unsigned = signed >= 0 ?
            UInt(signed) :
            UInt(signed  - Int.min) + UInt(Int.max) + 1
    
        return unsigned
    }
    

    // 将UInt8 转化为 int
    func convertToInt(unsigned: UInt) -> Int {
    let signed = (unsigned <= UInt(Int.max)) ?
    Int(unsigned) :
    Int(unsigned - UInt(Int.max) - 1) + Int.min

        return signed
    }

    相关文章

      网友评论

          本文标题:swift int 与UInt8相互转化

          本文链接:https://www.haomeiwen.com/subject/shtizltx.html