在Swift中将字符转换为Int [英] Convert Character to Int in Swift

查看:1103
本文介绍了在Swift中将字符转换为Int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,其中包括使用 Damm算法验证Int输入的校验和, a>。我设法创建了一个操作表,我的访问表中的值的方法涉及传递一个临时值和一个数字作为列值传入。
ex。

  self.tableToUse [interim,checkSumArray [i]] 
  func encode(number:Int) - > Int {
var checkSumArray = [Int]()
如果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 %(10000)-checkSumArray [2])/ b)
checkSumArray.append((number%(100000)-checkSumArray [3] -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
}
}

正如你所看到的,我不得不诉诸一个非常讨厌的方式来处理这个。它工作,但它是非常有限,低效,只是丑陋看或维护。我看过在我建立的Damm表中使用Characters而不是Ints的选项,改变get / set方法来处理这些,但这是一个额外的工作,并可能引入其他问题。



谢谢!

解决方案

没有必要使用字符,但是你的代码创建一个
数组与输入数字的十进制数字可以大大
简化:

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


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!

解决方案

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()  

这篇关于在Swift中将字符转换为Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆