4

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!

kellanburket
  • 12,250
  • 3
  • 46
  • 73
beatsbears
  • 201
  • 2
  • 7
  • 1
    The title of this question is so confusing. Had to read this question a couple of times to find that you're actually looking for "suggestions of alternative ways to handle this" : changing a number into an array of digits – Krzak Mar 03 '15 at 09:35
  • possible duplicate of [How convert a \*positive\* number into an array of digits in Swift](http://stackoverflow.com/questions/27736317/how-convert-a-positive-number-into-an-array-of-digits-in-swift) – Krzak Mar 03 '15 at 09:53
  • You're right, @Krzak, after reading this an alternative title makes much more sense. A combination of the solutions provided have greatly improved my function. Thanks all! – beatsbears Mar 03 '15 at 13:55

3 Answers3

2
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
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
2

Based on How convert a *positive* number into an array of digits in Swift, you could do (the number has to be positive):

let checkSumArray = map(number.description) { String($0).toInt()! }
Community
  • 1
  • 1
Krzak
  • 1,441
  • 11
  • 12
1

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()  
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382