hex

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2025 License: BSD-3-Clause Imports: 4 Imported by: 0

README

hex GoDoc

Lua module for encoding/hex

Usage

Encoding
local hex = require("hex")

s = hex.RawStdEncoding:encode_to_string("foo\01bar")
print(s)
Zm9vAWJhcg

s = hex.StdEncoding:encode_to_string("foo\01bar")
print(s)
Zm9vAWJhcg==

s = hex.RawURLEncoding:encode_to_string("this is a <tag> and should be encoded")
print(s)
dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA

s = hex.URLEncoding:encode_to_string("this is a <tag> and should be encoded")
print(s)
dGhpcyBpcyBhIDx0YWc-IGFuZCBzaG91bGQgYmUgZW5jb2RlZA==

Decoding
local hex = require 'hex'

decoded, err = hex.decode_string("666f6f62617262617a")
assert(not err, err)
print(decoded)
foobar

encoded = hex.encode_to_string(decoded)
print(encoded)
666f6f62617262617a

Documentation

Overview

Package hex implements hex encode/decode functionality for lua.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeString

func DecodeString(L *lua.LState) int

DecodeString decodes the encoded string with the encoding

Example
state := lua.NewState()
defer state.Close()
Preload(state)
source := `
    local hex = require 'hex'
	s, err = hex.decode_string("666f6f01626172")
	assert(not err, err)
	print(s)
`
if err := state.DoString(source); err != nil {
	log.Fatal(err.Error())
}
Output:

foo�bar

func EncodeToString

func EncodeToString(L *lua.LState) int

EncodeToString decodes the string with the encoding

Example
state := lua.NewState()
defer state.Close()
Preload(state)
source := `
    local hex = require 'hex'
	s = hex.encode_to_string("foo\01bar")
	print(s)
`
if err := state.DoString(source); err != nil {
	log.Fatal(err.Error())
}
Output:

666f6f01626172

func LVHexDecoder

func LVHexDecoder(L *lua.LState, reader io.Reader) lua.LValue

LVHexDecoder creates a new Lua user data for the hex decoder

func LVHexEncoder

func LVHexEncoder(L *lua.LState, writer io.Writer) lua.LValue

LVHexEncoder creates a new Lua user data for the hex encoder

func Loader

func Loader(L *lua.LState) int

Loader is the module loader function.

func NewDecoder

func NewDecoder(L *lua.LState) int
Example
state := lua.NewState()
defer state.Close()
Preload(state)
strings.Preload(state)
source := `
    local hex = require 'hex'
	local strings = require 'strings'
    local reader = strings.new_reader('666f6f01626172')
	decoder = hex.new_decoder(reader)
    local s = decoder:read("*a")
	print(s)
`
if err := state.DoString(source); err != nil {
	log.Fatal(err.Error())
}
Output:

foo�bar

func NewEncoder

func NewEncoder(L *lua.LState) int
Example
state := lua.NewState()
defer state.Close()
Preload(state)
strings.Preload(state)
source := `
    local hex = require 'hex'
	local strings = require 'strings'
    local writer = strings.new_builder()
	encoder = hex.new_encoder(writer)
	encoder:write("foo\01bar")
	print(writer:string())
`
if err := state.DoString(source); err != nil {
	log.Fatal(err.Error())
}
Output:

666f6f01626172

func Preload

func Preload(L *lua.LState)

Preload adds hex to the given Lua state's package.preload table. After it has been preloaded, it can be loaded using require:

local hex = require("hex")

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL