Sample code for 30+ languages & platforms
Lua Requires Chilkat v9.5.0.64+

Demonstrate Binary Data Encoding Methods

Demonstrates binary data encoding methods.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local binData = chilkat.BinData.new()

-- Append 10 bytes: 00, 01, 02, 03, 04, 05, 06, 07, 08, 09
binData:append_encoded("00010203040506070809", "hex")

-- Show that binData contains 10 bytes.
print("num bytes = " .. binData.num_bytes)

-- Get as base64
print("base64: " .. binData:get_encoded("base64"))

-- Get a chunk of the binary data.
-- The 1st byte is at index 0.
-- The output should be "02030405"
local offset = 2
local numBytes = 4
print("chunk: " .. binData:get_encoded_chunk(offset, numBytes, "hex"))

-- Copy the bytes to a StringBuilder using the base64url encoding
local sb = chilkat.StringBuilder.new()
binData:get_encoded_sb("base64url", sb)
print("base64url: " .. sb:get_as_string())

-- Remove a chunk from the binary data
offset = 2
numBytes = 4
binData:remove_chunk(offset, numBytes)
-- The bytes remaining are 00,01,06,07,08,and 09
print("after removing chunk: " .. binData:get_encoded("hex"))