英文:
Go - convert string which represent binary number into int
问题
我为此写了一个愚蠢的解决方案,有更好的方法吗?
如你所见,有很多无用的转换。
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"))
}
英文:
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"))
}
答案1
得分: 45
你需要strconv.ParseInt
函数,它可以将任意进制的字符串转换为给定位数的整数。
package main
import (
"fmt"
"strconv"
)
func main() {
if i, err := strconv.ParseInt("1001", 2, 64); err != nil {
fmt.Println(err)
} else {
fmt.Println(i)
}
}
英文:
You want the strconv.ParseInt
function, which converts from an arbitrary base, into a given bit size.
package main
import (
"fmt"
"strconv"
)
func main() {
if i, err := strconv.ParseInt("1001", 2, 64); err != nil {
fmt.Println(err)
} else {
fmt.Println(i)
}
}
答案2
得分: 8
例如,在Go 1上,
package main
import (
"fmt"
"strconv"
)
func main() {
i, err := strconv.ParseInt("1101", 2, 64)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(i)
}
输出:
13
英文:
For example, on Go 1,
package main
import (
"fmt"
"strconv"
)
func main() {
i, err := strconv.ParseInt("1101", 2, 64)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(i)
}
Output:
13
答案3
得分: 0
package main
import (
"fmt"
)
var digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
func convertBase(num, fromBase, toBase int) string {
if num == 0 {
return "0"
}
var result []byte
n := num
if n < 0 {
n = -n
result = append(result, '-')
}
var stack []int
for n > 0 {
remainder := n % toBase
stack = append(stack, remainder)
n /= toBase
}
for len(stack) > 0 {
result = append(result, digits[stack[len(stack)-1]])
stack = stack[:len(stack)-1]
}
return string(result)
}
func main() {
var num, fromBase, toBase int
fmt.Print("Enter the number: ")
fmt.Scanln(&num)
fmt.Print("Enter the base of origin: ")
fmt.Scanln(&fromBase)
fmt.Print("Enter the base destination: ")
fmt.Scanln(&toBase)
// Convert from the original base to decimal first
decimalNum := 0
n := num
p := 1
for n > 0 {
remainder := n % 10
decimalNum += remainder * p
n /= 10
p *= fromBase
}
// Convert the decimal number to the desired base
result := convertBase(decimalNum, 10, toBase)
fmt.Printf("%v based on %v = %v based on %v.\n", num, fromBase, result, toBase)
}
英文:
package main
import (
"fmt"
)
var digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
func convertBase(num, fromBase, toBase int) string {
if num == 0 {
return "0"
}
var result []byte
n := num
if n < 0 {
n = -n
result = append(result, '-')
}
var stack []int
for n > 0 {
remainder := n % toBase
stack = append(stack, remainder)
n /= toBase
}
for len(stack) > 0 {
result = append(result, digits[stack[len(stack)-1]])
stack = stack[:len(stack)-1]
}
return string(result)
}
func main() {
var num, fromBase, toBase int
fmt.Print("Enter the number: ")
fmt.Scanln(&num)
fmt.Print("Enter the base of origin: ")
fmt.Scanln(&fromBase)
fmt.Print("Enter the base destination: ")
fmt.Scanln(&toBase)
// Convert from the original base to decimal first
decimalNum := 0
n := num
p := 1
for n > 0 {
remainder := n % 10
decimalNum += remainder * p
n /= 10
p *= fromBase
}
// Convert the decimal number to the desired base
result := convertBase(decimalNum, 10, toBase)
fmt.Printf("%v based on %v = %v based on %v.\n", num, fromBase, result, toBase)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论