Convert rune to int

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.

a := 'a'
n := int(a-'0')

  

posted @ 2019-12-27 09:32  闲云-野鹤  阅读(569)  评论(0编辑  收藏  举报