英文:
Convert rune to int?
问题
在下面的代码中,我逐个字符(rune)遍历一个字符串(string),但实际上我需要一个整数(int)来执行一些校验和计算。我真的需要将rune编码为[]byte,然后将其转换为字符串,然后使用Atoi从rune中获取一个整数吗?这是惯用的做法吗?
// 字符串s只包含数字。
var factor int
for i, c := range s[:12] {
if i % 2 == 0 {
factor = 1
} else {
factor = 3
}
buf := make([]byte, 1)
_ = utf8.EncodeRune(buf, c)
value, _ := strconv.Atoi(string(buf))
sum += value * factor
}
在playground上查看:http://play.golang.org/p/noWDYjn5rJ
英文:
In the following code, I iterate over a string rune by rune, but I'll actually need an int to perform some checksum calculation. Do I really need to encode the rune into a []byte, then convert it to a string and then use Atoi to get an int out of the rune? Is this the idiomatic way to do it?
// The string `s` only contains digits.
var factor int
for i, c := range s[:12] {
    if i % 2 == 0 {
        factor = 1
    } else {
        factor = 3
    }
    buf := make([]byte, 1)
    _ = utf8.EncodeRune(buf, c)
    value, _ := strconv.Atoi(string(buf))
    sum += value * factor
}
On the playground: http://play.golang.org/p/noWDYjn5rJ
答案1
得分: 129
问题比看起来的要简单。你可以使用int(r)将一个rune值转换为一个int值。但是你的代码暗示你想要从数字的ASCII(或UTF-8)表示中获取整数值,你可以通过r - '0'作为一个rune或int(r - '0')作为一个int来轻松地实现。请注意,超出范围的rune将破坏这个逻辑。
英文:
The problem is simpler than it looks. You convert a rune value to an int value with int(r). But your code implies you want the integer value out of the ASCII (or UTF-8) representation of the digit, which you can trivially get with r - '0' as a rune, or int(r - '0') as an int. Be aware that out-of-range runes will corrupt that logic.
答案2
得分: 14
例如,sum += (int(c) - '0') * factor,
package main
import (
	"fmt"
	"strconv"
	"unicode/utf8"
)
func main() {
	s := "9780486653556"
	var factor, sum1, sum2 int
	for i, c := range s[:12] {
		if i%2 == 0 {
			factor = 1
		} else {
			factor = 3
		}
		buf := make([]byte, 1)
		_ = utf8.EncodeRune(buf, c)
		value, _ := strconv.Atoi(string(buf))
		sum1 += value * factor
		sum2 += (int(c) - '0') * factor
	}
	fmt.Println(sum1, sum2)
}
输出:
124 124
英文:
For example, sum += (int(c) - '0') * factor,
package main
import (
	"fmt"
	"strconv"
	"unicode/utf8"
)
func main() {
	s := "9780486653556"
	var factor, sum1, sum2 int
	for i, c := range s[:12] {
		if i%2 == 0 {
			factor = 1
		} else {
			factor = 3
		}
		buf := make([]byte, 1)
		_ = utf8.EncodeRune(buf, c)
		value, _ := strconv.Atoi(string(buf))
		sum1 += value * factor
		sum2 += (int(c) - '0') * factor
	}
	fmt.Println(sum1, sum2)
}
Output:
124 124
答案3
得分: 2
为什么你不只使用 "string(rune)"?
s := "12345678910"
var factor, sum int
for i, x := range s {
if i%2 == 0 {
factor = 1
} else {
factor = 3
}
xstr := string(x) // x 是 rune 转换为字符串
xint, _ := strconv.Atoi(xstr)
sum += xint * factor
}
fmt.Println(sum)
英文:
why don't you do only "string(rune)".
s:="12345678910"
var factor,sum int
for i,x:=range s{
	if i%2==0{
     		factor=1
     	}else{
		factor=3
	}
     	xstr:=string(x) //x is rune converted to string
     	xint,_:=strconv.Atoi(xstr)
     	sum+=xint*factor
}
fmt.Println(sum)
答案4
得分: -1
val, _ := strconv.Atoi(string(v))
其中v是一个rune
更简洁但与上述代码思路相同
英文:
val, _ := strconv.Atoi(string(v))
Where v is a rune
More concise but same idea as above
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论