Golang数字进制转换

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

Golang number base conversion

问题

在Golang中,如果不使用strconv包,你可以使用以下方法将一个十进制数从一种进制转换为另一种进制:

  1. 首先,将十进制数转换为目标进制的字符串表示。你可以使用循环和取余操作来实现这一点。例如,如果你要将一个十进制数转换为二进制,你可以使用以下代码:
func convertToBase(num, base int) string {
    result := ""
    for num > 0 {
        remainder := num % base
        result = strconv.Itoa(remainder) + result
        num = num / base
    }
    return result
}

func main() {
    decimalNum := 42
    binaryNum := convertToBase(decimalNum, 2)
    fmt.Println(binaryNum) // 输出:101010
}
  1. 如果你需要将目标进制的字符串表示转换为整数,你可以使用类似的方法。例如,如果你要将一个二进制数转换为十进制,你可以使用以下代码:
func convertToDecimal(num string, base int) int {
    result := 0
    power := 1
    for i := len(num) - 1; i >= 0; i-- {
        digit := int(num[i] - '0')
        result += digit * power
        power *= base
    }
    return result
}

func main() {
    binaryNum := "101010"
    decimalNum := convertToDecimal(binaryNum, 2)
    fmt.Println(decimalNum) // 输出:42
}

这些方法可以帮助你在不使用strconv包的情况下在不同进制之间进行转换。希望对你有所帮助!

英文:

I was wondering, how do you convert a base10 number from one base to another without usage of strconv in Golang ?

Could you please give me some advice ?

答案1

得分: 8

package main

import (
    "fmt"
    "math/big"
)

func main() {
    fmt.Println(big.NewInt(1000000000000).Text(62))
}

演示

英文:
package main

import (
    "fmt"
    "math/big"
)

func main() {
    fmt.Println(big.NewInt(1000000000000).Text(62))
}

Demo

答案2

得分: 3

使用 math 包和一个对数 公式

log_77(x) = log(x) / log(77)

英文:

Use the math package and a log identify:

log_77(x) = log(x) / log(77)

答案3

得分: 2

这可能算作弊,但我猜你可以查看strconv.FormatInt的实现,并根据它构建一些自己的代码作为示例。这样你就不是直接使用它,而是自己实现了它。

英文:

This is probably cheating but I guess you could look at the implementation of strconv.FormatInt, and build some of your own code using that as an example. That way you aren't using it directly, you have implemented it yourself.

答案4

得分: 1

你可以使用这个函数将任何十进制数转换为任意基数,并选择字符集。

func encode(nb uint64, buf *bytes.Buffer, base string) {
    l := uint64(len(base))
    if nb/l != 0 {
        encode(nb/l, buf, base)
    }
    buf.WriteByte(base[nb%l])
}

func decode(enc, base string) uint64 {
    var nb uint64
    lbase := len(base)
    le := len(enc)
    for i := 0; i < le; i++ {
        mult := 1
        for j := 0; j < le-i-1; j++ {
            mult *= lbase
        }
        nb += uint64(strings.IndexByte(base, enc[i]) * mult)
    }
    return nb
}

你可以像这样使用它:

// 编码
var buf bytes.Buffer
encode(100, &buf, "0123456789abcdef")
fmt.Println(buf.String())
// 输出:64

// 解码
val := decode("64", "0123456789abcdef")
fmt.Println(val)
// 输出:100
英文:

You can use this function to convert any decimal number to any base with the character set of your choice.

func encode(nb uint64, buf *bytes.Buffer, base string) {
  l := uint64(len(base))
  if nb/l != 0 {
	encode(nb/l, buf, base)
  }
  buf.WriteByte(base[nb%l])
}

func decode(enc, base string) uint64 {
  var nb uint64
  lbase := len(base)
  le := len(enc)
  for i := 0; i &lt; le; i++ {
	mult := 1
	for j := 0; j &lt; le-i-1; j++ {
		mult *= lbase
	}
	nb += uint64(strings.IndexByte(base, enc[i]) * mult)
  }
  return nb
}

You can use it like that:

// encoding
var buf bytes.Buffer
encode(100, &amp;buf, &quot;0123456789abcdef&quot;)
fmt.Println(buf.String())
// 64

// decoding
val := decode(&quot;64&quot;, &quot;0123456789abcdef&quot;)
fmt.Println(val)
// 100

huangapple
  • 本文由 发表于 2017年2月2日 15:50:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/41996761.html
匿名

发表评论

匿名网友

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

确定