philiprehberger-base_convert
Multi-format base encoding with Base32, Base36, Base58, Base62, and Base85 support
Requirements
- Ruby >= 3.1
Installation
Add to your Gemfile:
gem "philiprehberger-base_convert"
Or install directly:
gem install philiprehberger-base_convert
Usage
require "philiprehberger/base_convert"
# Base58 (Bitcoin alphabet) — encode/decode strings
encoded = Philiprehberger::BaseConvert.base58_encode('Hello World')
decoded = Philiprehberger::BaseConvert.base58_decode(encoded)
Base62
Encode and decode integers using the URL-safe 0-9A-Za-z alphabet:
Philiprehberger::BaseConvert.base62_encode(123_456) # => "W7E"
Philiprehberger::BaseConvert.base62_decode('W7E') # => 123456
Base32 (Crockford)
Case-insensitive encoding that excludes I, L, O, U to avoid ambiguity:
Philiprehberger::BaseConvert.base32_encode('Hello') # => "91JPRV3F"
Philiprehberger::BaseConvert.base32_decode('91JPRV3F') # => "Hello"
Base85 (ASCII85)
Compact binary-to-text encoding:
Philiprehberger::BaseConvert.base85_encode('Hello') # => "87cURDZ"
Philiprehberger::BaseConvert.base85_decode('87cURDZ') # => "Hello"
Base36
Encode and decode integers using the 0-9A-Z alphabet (alphanumeric, case-sensitive uppercase):
Philiprehberger::BaseConvert.base36_encode(123_456) # => "2N9C"
Philiprehberger::BaseConvert.base36_decode('2N9C') # => 123456
Hex
require "philiprehberger/base_convert"
Philiprehberger::BaseConvert.hex_encode("Hello") # => "48656c6c6f"
Philiprehberger::BaseConvert.hex_decode("48656c6c6f") # => "Hello"
Detect encoding
Inspect the input alphabet and return the narrowest matching base, or nil when no single base covers all characters:
Philiprehberger::BaseConvert.detect('deadbeef') # => :hex
Philiprehberger::BaseConvert.detect('HELLOWORLD') # => :base32
Philiprehberger::BaseConvert.detect('AUVW') # => :base58
Philiprehberger::BaseConvert.detect('0U9l') # => :base62
Philiprehberger::BaseConvert.detect('SGVsbG9Xb3JsZA==') # => :base64
Philiprehberger::BaseConvert.detect('#$%&()') # => :base85
Philiprehberger::BaseConvert.detect('') # => nil
Philiprehberger::BaseConvert.detect('hello world') # => nil
Arbitrary Base
Encode and decode integers in any base from 2 to 62:
Philiprehberger::BaseConvert.encode(255, base: 16) # => "FF"
Philiprehberger::BaseConvert.decode('FF', base: 16) # => 255
Philiprehberger::BaseConvert.encode(42, base: 2) # => "101010"
API
| Method | Description |
|---|---|
BaseConvert.base58_encode(string) |
Encode a string to Base58 (Bitcoin alphabet) |
BaseConvert.base58_decode(string) |
Decode a Base58 string |
BaseConvert.base62_encode(integer) |
Encode an integer to Base62 |
BaseConvert.base62_decode(string) |
Decode a Base62 string to an integer |
BaseConvert.base32_encode(string) |
Encode a string to Crockford Base32 |
BaseConvert.base32_decode(string) |
Decode a Crockford Base32 string |
BaseConvert.base85_encode(string) |
Encode a string to ASCII85 |
BaseConvert.base85_decode(string) |
Decode an ASCII85 string |
BaseConvert.base36_encode(integer) |
Encode an integer to Base36 |
BaseConvert.base36_decode(string) |
Decode a Base36 string to an integer |
hex_encode(string) |
Encode string to hexadecimal |
hex_decode(string) |
Decode hexadecimal to string |
BaseConvert.encode(integer, base:) |
Encode an integer in an arbitrary base (2-62) |
BaseConvert.decode(string, base:) |
Decode a string from an arbitrary base to an integer |
BaseConvert.detect(string) |
Detect the narrowest matching base (:hex, :base32, :base58, :base62, :base64, :base85) or nil |
Development
bundle install
bundle exec rspec
bundle exec rubocop
Support
If you find this project useful: