简体   繁体   中英

Convert Character to Int in Swift

I'm working on a project which includes verifying the checksum of an Int input with the Damm Algorithm . I've managed to create a the operational table and my method for accessing the value in the table involves passing an interim value and a digit to pass in as the column value. ex.

     self.tableToUse[interim,checkSumArray[i]]

Unfortunately, I've run into a snag when I'm trying to pass the digits from my input into the the get/set method where I cannot find a way to convert the Characters into Ints.

    func encode(number: Int) -> Int{
    var checkSumArray = [Int]()
    if number > 99999999 {
        println("number is too large")
        return 0
    }
    else if number < 0 {
        println("invalid input")
        return 0
    }
    else {
        checkSumArray.append(number%(10))
        checkSumArray.append((number%(100)-checkSumArray[0])/10)
        checkSumArray.append((number%(1000)-checkSumArray[1])/100)
        checkSumArray.append((number%(10000)-checkSumArray[2])/1000)
        checkSumArray.append((number%(100000)-checkSumArray[3])/10000)
        checkSumArray.append((number%(1000000)-checkSumArray[4])/100000)
        checkSumArray.append((number%(10000000)-checkSumArray[5])/1000000)
        checkSumArray.append((number%(100000000)-checkSumArray[6])/10000000)
        checkSumArray = checkSumArray.reverse()

        var interim: Int = 0

        for i in 0..<checkSumArray.count{
            interim = self.tableToUse[interim,checkSumArray[i]]
        }
        return interim
    }
}

As you can see, I've had to resort to a really nasty way of dealing with this. It works, but it's very limited, inefficient, and just ugly to look at or maintain. I've looked at the option of using Characters instead of Ints in the Damm Table I've constructed and altering the get/set method to deal with those instead, but that's a lot of extra work and could introduce other issues. Any suggestions of alternative ways to handle this, or a way to convert Characters to Ints would be appreciated.

Thanks!

if let int = Int(String(Character("1"))) {
    print(int)
}

You can also create a character extension as follow:

extension Character {
    var integerValue: Int? {
        return Int(String(self)) 
    }
}

Testing

Character("1").integerValue  // 1
Character("2").integerValue  // 2
Character("3").integerValue  // 3
Character("4").integerValue  // 4
Character("5").integerValue  // 5
Character("6").integerValue  // 6
Character("7").integerValue  // 7
Character("8").integerValue  // 8
Character("9").integerValue  // 9
Character("0").integerValue  // 0
Character("a").integerValue  // nil

Array("9876").first!.integerValue  // 9
Array("9876")[1].integerValue      // 8
Array("9876")[2].integerValue      // 7
Array("9876").last!.integerValue   // 6

edit/update Swift 5

Swift 5 adds many new properties to the Character and one of them fits exactly to this purpose. It is called wholeNumberValue

Character("1").wholeNumberValue  // 1
Character("2").wholeNumberValue  // 2
Character("3").wholeNumberValue  // 3
Character("4").wholeNumberValue  // 4
Character("④").wholeNumberValue  // 4
Character("5").wholeNumberValue  // 5
Character("6").wholeNumberValue  // 6
Character("7").wholeNumberValue  // 7
Character("8").wholeNumberValue  // 8
Character("9").wholeNumberValue  // 9
Character("0").wholeNumberValue  // 0
Character("万").wholeNumberValue  // 10_000
Character("a").wholeNumberValue  // nil

基于如何将一个*正*数转换为Swift中的数字数组 ,你可以做(​​数字必须是正数):

let checkSumArray = map(number.description) { String($0).toInt()! }

There is no need to work with characters, but your code to create an array with the decimal digits of the input number can be greatly simplified:

var checkSumArray = [Int]()
var tmp = number
while tmp > 0 {
    checkSumArray.append(tmp % 10)
    tmp /= 10
}
checkSumArray = checkSumArray.reverse()  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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