将rune转换为int?

huangapple go评论108阅读模式
英文:

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?

  1. // The string `s` only contains digits.
  2. var factor int
  3. for i, c := range s[:12] {
  4. if i % 2 == 0 {
  5. factor = 1
  6. } else {
  7. factor = 3
  8. }
  9. buf := make([]byte, 1)
  10. _ = utf8.EncodeRune(buf, c)
  11. value, _ := strconv.Atoi(string(buf))
  12. sum += value * factor
  13. }

On the playground: http://play.golang.org/p/noWDYjn5rJ

答案1

得分: 129

问题比看起来的要简单。你可以使用int(r)将一个rune值转换为一个int值。但是你的代码暗示你想要从数字的ASCII(或UTF-8)表示中获取整数值,你可以通过r - '0'作为一个runeint(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

  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. "unicode/utf8"
  6. )
  7. func main() {
  8. s := "9780486653556"
  9. var factor, sum1, sum2 int
  10. for i, c := range s[:12] {
  11. if i%2 == 0 {
  12. factor = 1
  13. } else {
  14. factor = 3
  15. }
  16. buf := make([]byte, 1)
  17. _ = utf8.EncodeRune(buf, c)
  18. value, _ := strconv.Atoi(string(buf))
  19. sum1 += value * factor
  20. sum2 += (int(c) - '0') * factor
  21. }
  22. fmt.Println(sum1, sum2)
  23. }

输出:

  1. 124 124
英文:

For example, sum += (int(c) - '0') * factor,

  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. "unicode/utf8"
  6. )
  7. func main() {
  8. s := "9780486653556"
  9. var factor, sum1, sum2 int
  10. for i, c := range s[:12] {
  11. if i%2 == 0 {
  12. factor = 1
  13. } else {
  14. factor = 3
  15. }
  16. buf := make([]byte, 1)
  17. _ = utf8.EncodeRune(buf, c)
  18. value, _ := strconv.Atoi(string(buf))
  19. sum1 += value * factor
  20. sum2 += (int(c) - '0') * factor
  21. }
  22. fmt.Println(sum1, sum2)
  23. }

Output:

  1. 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)".

  1. s:="12345678910"
  2. var factor,sum int
  3. for i,x:=range s{
  4. if i%2==0{
  5. factor=1
  6. }else{
  7. factor=3
  8. }
  9. xstr:=string(x) //x is rune converted to string
  10. xint,_:=strconv.Atoi(xstr)
  11. sum+=xint*factor
  12. }
  13. fmt.Println(sum)

答案4

得分: -1

val, _ := strconv.Atoi(string(v))

其中v是一个rune

更简洁但与上述代码思路相同

英文:
  1. val, _ := strconv.Atoi(string(v))

Where v is a rune

More concise but same idea as above

huangapple
  • 本文由 发表于 2014年1月24日 08:27:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/21322173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定