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: about 1 month 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 (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-07-09T15:09:16.000Z (11 months ago)
- Last Synced: 2024-01-31T05:16:03.161Z (4 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
Lists
- awesome-go - basexx - Convert to, from, and between digit strings in various number bases. (Miscellaneous / Uncategorized)
- 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)
- awesome-go - basexx - Convert to, from, and between digit strings in various number bases. (Miscellaneous / Uncategorized)
- awesome-go - basexx - Convert to, from, and between digit strings in various number bases. (Miscellaneous / Uncategorized)
- awesome-go-projects - basexx - Convert to, from, and between digit strings in various number bases. (Miscellaneous / Uncategorized)
- awesome-go - basexx - Convert to, from, and between digit strings in various number bases. (Miscellaneous / Uncategorized)
- awesome-go - basexx - Convert to, from, and between digit strings in various number bases. (Miscellaneous / Uncategorized)
- awesome-go-cn - basexx
- zero-alloc-awesome-go - basexx - Convert to, from, and between digit strings in various number bases. (Miscellaneous / Uncategorized)
- awesome-go-zh - basexx
- awesome-go-with-stars - basexx - Convert to, from, and between digit strings in various number bases. (Miscellaneous / Uncategorized)
- repo-1316-awesome-go-cn - basexx
- repo-1211-awesome-go-cn - basexx
- Go-awesome - basexx - Convert to, from, and between digit strings in various number bases. (Miscellaneous / Uncategorized)
- awesome-go-cn - basexx
- awesome-go - basexx - Convert to, from, and between digit strings in various number bases. (Miscellaneous / Uncategorized)
- awesome-go - basexx - Convert to, from, and between digit strings in various number bases. (Miscellaneous / Uncategorized)
- awesome-go-stars - basexx - Convert to, from, and between digit strings in various number bases. (Miscellaneous / Uncategorized)
- awesome-go - basexx - Convert to, from, and between digit strings in various number bases. (Miscellaneous / Uncategorized)
- awesome-go - basexx - Convert to, from, and between digit strings in various number bases. (Miscellaneous / 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’))
}
```