I wrote a stupid solution for this, any better recipe?
As you can see lots of useless conversions there.
package main
import (
"fmt"
"strconv"
"math"
)
func conv(str string) int {
l := len(str)
result := 0.0
for i,n := range str {
number,_ := strconv.Atof64(string(n))
result += math.Exp2(float64(l-i-1))*number
}
return int(result)
}
func main() {
fmt.Println(conv("1001"))
}
You want the
strconv.ParseIntfunction, which converts from an arbitrary base, into a given bit size.Playground