https://github.com/ctison/baseconverter
Golang package for base conversion
https://github.com/ctison/baseconverter
base
Last synced: 11 months ago
JSON representation
Golang package for base conversion
- Host: GitHub
- URL: https://github.com/ctison/baseconverter
- Owner: ctison
- Created: 2018-08-31T22:25:36.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2021-11-26T17:09:38.000Z (over 4 years ago)
- Last Synced: 2023-07-14T04:49:39.173Z (almost 3 years ago)
- Topics: base
- Language: Go
- Homepage:
- Size: 23.4 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Golang package baseconverter
[](https://godoc.org/github.com/chtison/baseconverter)
Package baseconverter is a set of functions which perform base conversion.
## Quickstart
A **number** is represented as a \*math/big.Int in decimal base or as a string (interpreted as UTF-8 encoded) in any base.
```go
var number1 *big.Int = big.NewInt(42) // decimal base (base 10)
var number2 string = "this is a number" // this is a number
var base string = "this anumber" // this could be the base of number above
```
A **base** is represented as a string (interpreted as UTF-8 encoded), and must own at least two different runes.
```go
var base1 string = "0123456789" // decimal base
var base2 string = "0123456789ABCDEF" // hexadecimal base
var base3 string = "01" // base 2
var base4 string = "xy" // base 2
```
#### For example, you can convert a decimal number to base 16:
```go
package main
import (
"fmt"
bc "github.com/chtison/baseconverter"
)
func main() {
nbrInBase16, _ := bc.UInt64ToBase(51966, "0123456789ABCDEF")
fmt.Println(nbrInBase16)
}
```
> CAFE
#### Or convert back a number in base "01" (base 2) to base 10:
```go
package main
import (
"fmt"
bc "github.com/chtison/baseconverter"
)
func main() {
nbr, _ := bc.BaseToDecimal("101010", "01")
fmt.Println(nbr)
}
```
> 42
#### Or convert a number from any base to any other:
```go
package main
import (
"fmt"
bc "github.com/chtison/baseconverter"
)
func main() {
var number string = "π΄ππππππππ±ππππ΅π±ππ΄πΌπ΅ππ±ππΌ"
var inBase string = "π΅π±ππππππ°πΌππ΅π΄πππ"
var toBase string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !"
converted, _, _ := bc.BaseToBase(number, inBase, toBase)
fmt.Println(converted)
}
```
> Hello Gophers !