crockford

Version, currently master branch1 version
  • master branchlatestFeb 27, 2022

github.com/davidkellis/crockford

Crystal implementation of Crockford's base-32 encoding.

0 stars
0 dependents
License: MIT

Installation

# Add this to your shard.yml
dependencies:
  crockford:
    github: davidkellis/crockford
    branch: master

master is a branch, not a release, so this tracks it rather than pinning a version.

Then run:

shards install

shard.yml

Crystal
1.3.0
License
MIT
Author
David Ellis

Dependencies

This version declares no dependencies.

README

crockford

This library implements Crockford's base 32 encoding as defined at https://www.crockford.com/base32.html.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      crockford:
        github: davidkellis/crockford
  2. Run shards install

Usage

There are three methods that make up the API:

  • Crockford.encode(number : Int, encoding : Encoding = Crockford::DEFAULT) : String

    Takes a non-negative integer and encodes it with Crockford's base-32 encoding. Returns a String representing the encoded value.

  • Crockford.decode(str : String, encoding : Encoding = Crockford::DEFAULT) : UInt64

    Decodes the given Crockford base-32 encoded string. Raises ArgumentError if the encoded string value does not represent a valid encoded value. Returns a UInt64 representation of the encoded value.

  • Crockford.decode?(str : String, encoding : Encoding = Crockford::DEFAULT) : UInt64?

    Decodes the given Crockford base-32 encoded string. Returns a UInt64 representation of the encoded value if the encoded string represents a valid encoded value; otherwise, returns nil.

require "crockford"

encoded_value = Crockford.encode(123)               # encoded_value = "3V"
decoded_value = Crockford.decode(encoded_value)     # decoded_value = 123

Crockford.encode(-5)                                # raises ArgumentError, "Unable to encode negative values."

Crockford.decode("invalid_value")                   # raises ArgumentError, "Unable to decode ordinal 95."

optional_val = Crockford.decode?("invalid_value")   # optional_val = nil

Contributors