Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bobg/basexx
Convert digit strings between arbitrary bases.
https://github.com/bobg/basexx
Last synced: 3 months ago
JSON representation
Convert digit strings between arbitrary bases.
- Host: GitHub
- URL: https://github.com/bobg/basexx
- Owner: bobg
- License: mit
- Created: 2019-06-08T17:46:13.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-07-09T15:09:16.000Z (over 1 year ago)
- Last Synced: 2024-07-31T20:52:23.412Z (6 months ago)
- Language: Go
- Homepage: https://godoc.org/github.com/bobg/basexx
- Size: 27.3 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - basexx - Convert to, from, and between digit strings in various number bases. (Miscellaneous / Uncategorized)
- zero-alloc-awesome-go - basexx - Convert to, from, and between digit strings in various number bases. (Miscellaneous / Uncategorized)
- awesome-go-extra - basexx - 06-08T17:46:13Z|2021-10-02T14:57:12Z| (Microsoft Office / Uncategorized)
README
# Basexx - Convert between digit strings and various number bases
[![Go Reference](https://pkg.go.dev/badge/github.com/bobg/v2/basexx.svg)](https://pkg.go.dev/github.com/bobg/basexx/v2)
[![Go Report Card](https://goreportcard.com/badge/github.com/bobg/basexx/v2)](https://goreportcard.com/report/github.com/bobg/basexx/v2)
[![Tests](https://github.com/bobg/basexx/actions/workflows/go.yml/badge.svg)](https://github.com/bobg/basexx/actions/workflows/go.yml)
[![Coverage Status](https://coveralls.io/repos/github/bobg/basexx/badge.svg?branch=master)](https://coveralls.io/github/bobg/basexx?branch=master)This is basexx,
a package for converting numbers to digit strings in various bases
and vice versa.## Usage
To get the Base30 encoding of the number 412:
```go
var sb strings.Builder
if err := basexx.EncodeInt64(&sb, 412, basexx.Base30); err != nil { ... }
result := sb.String()
```To decode the Base30 digit string `"fr"`:
```go
result, err := basexx.DecodeString("fr", basexx.Base30)
```To convert a digit string `x` in base `from` to a new digit string in base `to`:
```go
result, err := basexx.Convert(x, from, to)
```To define your own new number base:
```go
// ReverseBase10 uses digits '9' through '0' just to mess with you.
type ReverseBase10 struct{}func (ReverseBase10) N() int64 { return 10 }
func (ReverseBase10) Digit(val int64) (byte, error) {
if val < 0 || val > 9 {
return 0, errors.New("digit value out of range")
}
return byte('9' - val), nil
}func (ReverseBase10) Val(digit byte) (int64, error) {
if digit < '0’ || digit > '9' {
return 0, errors.New("invalid encoded digit")
}
return int64(9 - (digit - '0’))
}
```