Convert rune to int?

golang int to string
golang rune
int64 to string golang
golang rune to string
golang uint to string
convert rune array to string golang
golang string to float64
convert runes to string

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

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.

Golang: convert rune to int, For example suppose you want to convert the rune with the value 56 to an int, doing int(rune) will return 56. Buy 56 is the code of the integer 8. Query properties of a Rune. To get the integer code point value of a Rune instance, use the Rune.Value property. Rune rune = new Rune('\ud83d', '\udd2e'); // U+1F52E CRYSTAL BALL int codePoint = rune.Value; // = 128302 decimal (= 0x1F52E) Many of the static APIs available on the char type are also available on the Rune type.

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

Golang : convert rune to integer value, Just a short tutorial on how to convert a rune into integer. Converting rune to integer value can be useful for generating checksum to ensure that� x is a string and T is []byte or []rune. You want to convert x, of type int, int32, or int64, to T of type rune, an alias for type int32. x 's type and T are both integer types. Therefore, T (x) is allowed and is written rune (x), for your example, c = rune (i).

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)

Convert rune to int? - Golang, 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� Converting an Integer to a String. Just like before, the method of converting an int to a string in earlier versions of C++ differs from the one used in C++11 and above. Up to C++03. Users of this version can use the std::stringstream class from the C++ standard library. The following example converts the integer x into a string named out_string:

strconv, When converting to a shorter integer type, the value is truncated to fit in the is converted to a bool , int , float64 , complex128 , string or rune depending on the� 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.

Go: Conversions, What is Rune? How to runes := []rune(str) result = append(result, int(runes[i])) . } NumCPU returns the number of logical CPUs usable by the current process. It is the recommended way to convert string to an integer. The TryParse() method converts the string representation of a number to its 16, 32, and 64-bit signed integer equivalent. It returns boolean which indicates whether the conversion succeeded or failed and so it never throws exceptions.

What is Rune? How to get ASCII value of any character in Go , 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� Just for fun, you can type in any text below and then each letter will be transformed into their nearest Rune equivalent. This is not a translation but a transliteration of each group of letters as there is no easy way to perform a real translation from English into a true runic based language.

Comments
  • See also: Section 1.6 K&R2.
  • Thanks, but that's not quite what I want. You are initializing the rune with 42 - that would yield the * character according to my ASCII table. What I get is a string and range yields runes, that represent the the character. Using int on the rune would give me the value of the representation, not the display value, which I need. I made a short snippet, that hints at the difference: play.golang.org/p/JLlmKnddGv
  • @miku: see my revised answer.
  • Ah, nice trick. I don't know why your answer has been downvoted.