一个如何从基数10转换为任意基数2-36?
在JavaScript中,你只是
Number.toString(radix?)
Run Code Online (Sandbox Code Playgroud)
例如
变数= 12345; console.log(my_number.toString(36).toUpperCase());
在进行中,这是达到相同效果的方法
func convertToAnyBase2to36(num int64, base int64) string {
var len int64 = base
var source_string = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "J", "I", "J", "K",
"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X","Y","Z"}
code := "";
for {
if num <= 0 {
break
}
mod := num % len;
num = (num - mod) / len;
code = source_string[mod]+code;
}
return code
}
Run Code Online (Sandbox Code Playgroud)
例如
fmt.Println(convertToAnyBase2to36(12354,2))
Run Code Online (Sandbox Code Playgroud)
在Go中,您有strconv.FormatInt()(和strconv.FormatUint()):
fmt.Println(strconv.FormatInt(12345, 2))
fmt.Println(strconv.FormatInt(12345, 16))
fmt.Println(strconv.FormatInt(12345, 32))
Run Code Online (Sandbox Code Playgroud)
这将输出(在Go Playground上尝试):
11000000111001
3039
c1p
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
62 次 |
| 最近记录: |