项目作者: oploadk

项目描述 :
A pure Lua base-to-base converter
高级语言: Lua
项目地址: git://github.com/oploadk/base2base.git
创建时间: 2017-10-29T10:33:00Z
项目社区:https://github.com/oploadk/base2base

开源协议:MIT License

下载


base2base

CI Status

Presentation

Convert strings representing numbers between different bases.

Dependencies

None except Teal or Lua 5.3+.

Tests require cwtest.

Usage

You should not use base36. But let’s say you have legacy code to work with
and need to convert binary from and to base36:

  1. local base2base = require "base2base"
  2. local alphabet_b36 = base2base.ALPHABET_B62:sub(1,36)
  3. local from_b36 = base2base.converter(alphabet_b36, base2base.ALPHABET_B256)
  4. local to_b36 = base2base.converter(base2base.ALPHABET_B256, alphabet_b36)
  5. local bin = from_b36("yolo42")
  6. assert(to_b36(bin) == "yolo42")

You can also use the library to check if the string is a valid representation
for a given base:

  1. assert(from_b36:validate("f00b4r"))
  2. assert(not from_b36:validate("f00+b4r"))

For hexadecimal, there are faster functions available directly in the module.

  1. local bin = base2base.from_hex("1234dead")
  2. assert(base2base.to_hex(bin) == "1234dead")
  3. assert(base2base.is_hex("1234dead"))

There are also helpers for base36, so you can do this:

  1. local bin = base2base.from_b36("yolo42")
  2. assert(base2base.to_b36(bin) == "yolo42")
  3. assert(base2base.is_b36("yolo42"))

Finally, there are helpers for padded base64 and its base64 URL variant:

  1. base2base.to_b64("Ma") -- "TWE="
  2. base2base.from_b64("TWE=") -- "Ma"
  3. base2base.to_b64url("Ma") -- "TWE="
  4. base2base.from_b64url("TWE=") -- "Ma"

There are no is_* validation helpers for base64.

Copyright (c) Pierre Chapuis