logo
0
0
WeChat Login

base_n

A lightweight Go library for encoding and decoding values in arbitrary base_n numbering systems.

Features

  • Convert numbers to and from custom base_n representations
  • Support for negative numbers
  • Built-in presets for common bases:
    • Base62 (alphanumeric: 0-9, a-z, A-Z)
    • Base3 (0, 1, 2)
    • Special Base3 (0, o, O)
  • Create custom alphabets for specialized encoding schemes

Installation

go get github.com/muleiwu/base_n

Usage

Basic Usage

package main import ( "fmt" BaseN "github.com/muleiwu/base_n" ) func main() { // Create a Base62 encoder/decoder base62 := BaseN.NewBase62() // Encode an integer to Base62 string encoded := base62.Encode(12345) fmt.Println("Encoded:", encoded) // Decode a Base62 string back to integer decoded, err := base62.Decode(encoded) if err != nil { panic(err) } fmt.Println("Decoded:", decoded) }

Custom Alphabet

package main import ( "fmt" BaseN "github.com/muleiwu/base_n" ) func main() { // Create a custom Base with your own character set customBase := BaseN.NewBaseN([]byte("01")) // Binary // Encode and decode using the custom base encoded := customBase.Encode(10) fmt.Println("Encoded in binary:", encoded) // Outputs: 1010 decoded, _ := customBase.Decode(encoded) fmt.Println("Decoded:", decoded) // Outputs: 10 }

Predefined Bases

// Base3 (0, 1, 2) base3 := BaseN.NewBase3Number() // Special Base3 (0, o, O) base3Special := BaseN.NewBase30oO() // Base62 (0-9, a-z, A-Z) base62 := BaseN.NewBase62()

API Reference

Constructor Functions

  • NewBaseN(chars []byte) *BaseN: Create a new base_n encoder/decoder with custom characters
  • NewBase3Number() *BaseN: Create a Base3 encoder/decoder (0, 1, 2)
  • NewBase30oO() *BaseN: Create a special Base3 encoder/decoder (0, o, O)
  • NewBase62() *BaseN: Create a Base62 encoder/decoder (0-9, a-z, A-Z)

Methods

  • Encode(n int64) string: Convert an integer to a base_n string
  • Decode(s string) (int64, error): Convert a base_n string back to an integer

License

MIT

About

A lightweight Go library for encoding and decoding values in arbitrary base-n numbering systems.

96.00 KiB
0 forks0 stars1 branches1 TagREADMEMIT license
Language
Go100%