BinData Lua Reference Documentation

chilkat.BinData

Current Version: 11.7.0

Markdown version of this page for AI tools (v 11.7.0) · all AI docs

Chilkat.BinData

Build, transform, encode, decode, inspect, and save binary data.

Chilkat.BinData is a general-purpose mutable byte container used throughout Chilkat for working with binary content in memory. It can load bytes from files, byte arrays, strings, and encoded text; append additional data in many forms; retrieve content as raw bytes, chunks, text, or encoded strings; compute hashes; convert character encodings; validate UTF-8; remove byte ranges; and write the final result to files.

Load binary content

Load data from files, byte arrays, strings, encoded strings, or other Chilkat objects that produce binary output.

Append and build data

Append bytes, strings, integers, BOMs, counted strings, padded fields, encoded data, and other BinData objects.

Encode and decode

Convert binary data to and from text encodings such as Base64, hex, URL encoding, and other supported binary encodings.

Text and charset handling

Interpret bytes as text, append strings using a specified charset, convert character encodings, and validate UTF-8 content.

Inspect and modify bytes

Get byte counts, extract chunks, remove ranges, compare data, and work with specific portions of a binary buffer.

Hash and save output

Compute hashes for the contained data and write the resulting bytes to files or pass them to other Chilkat classes.

Common pattern: Load or append binary data, perform any needed encoding, decoding, hashing, charset conversion, or byte-level edits, then retrieve the result as bytes, text, an encoded string, or a file. Use BinData whenever a Chilkat method needs a mutable in-memory binary buffer.

Object Creation

local chilkat = require("chilkat")

local bd = chilkat.BinData.new()
local bd = chilkat.BinData.new()

Creates the object (chilkat.BinData() does the same). Lua's garbage collector frees it when it is no longer referenced; bd:dispose() frees it at once, and in Lua 5.4 and later local bd <close> = chilkat.BinData.new() frees it when the block ends. Calling a method of a disposed object raises an error.

Errors

A method that fails returns nil followed by the error text, as io.open does: local body, err = bd:method(...), where err is the object's last_error_text. Wrap the call in assert() to raise an error with that text instead. Methods that answer a question return true or false, numbers are returned as they are (check last_method_success where the description says a value means failure), and property reads and writes never fail for Chilkat reasons.

Misuse raises a Lua error: a wrong argument type or a missing argument (bad argument #1 to ...), an unknown property name, a write to a read-only property, an out-of-range integer, or an object of the wrong class.

Names are Chilkat's in snake_case: QuickGetStr is quick_get_str, LastErrorText is last_error_text. Properties are fields (bd.verbose_logging = true) and methods use the colon (bd:method(...)). Strings are UTF-8; binary data is a Lua string of bytes. Indexes are 0-based, as in every Chilkat API. Unlocking: call chilkat.unlock_bundle("...") once at startup.

Properties

DebugLogFilePath
-- strVal is a string
local strVal = bd.debug_log_file_path
bd.debug_log_file_path = strVal

If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.

Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.

Possible causes of hangs include:

  • A timeout property set to 0, indicating an infinite timeout.
  • A hang occurring within an event callback in the application code.
  • An internal bug in the Chilkat code causing the hang.

More Information and Examples
top
LastErrorHtml
-- strVal is a string (read-only)
local strVal = bd.last_error_html

Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorText
-- strVal is a string (read-only)
local strVal = bd.last_error_text

Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorXml
-- strVal is a string (read-only)
local strVal = bd.last_error_xml

Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastMethodSuccess
-- boolVal is a boolean
local boolVal = bd.last_method_success
bd.last_method_success = boolVal

Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.

top
NumBytes
-- intVal is an integer (read-only)
local intVal = bd.num_bytes
Introduced in version 9.5.0.51

The number of bytes contained within the object.

top
VerboseLogging
-- boolVal is a boolean
local boolVal = bd.verbose_logging
bd.verbose_logging = boolVal

If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
Version
-- strVal is a string (read-only)
local strVal = bd.version

Version of the component/library, such as "10.1.0"

More Information and Examples
top

Methods

AppendBd
-- bin_data is a chilkat.BinData
-- ok is true, or nil and err on failure
local ok, err = bd:append_bd(bin_data)
Introduced in version 9.5.0.62

Appends the contents of another BinData to this object.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
AppendBom
-- charset is a string
-- ok is true, or nil and err on failure
local ok, err = bd:append_bom(charset)
Introduced in version 9.5.0.62

Appends the appropriate BOM (byte order mark), also known as a preamble, for the given charset. If the charset has no defined BOM, then nothing is appended. An application would typically call this to append the utf-8, utf-16, or utf-32 BOM.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
AppendByte
-- byte_value is an integer
-- ok is true, or nil and err on failure
local ok, err = bd:append_byte(byte_value)
Introduced in version 9.5.0.77

Appends a single byte. The byte_value should be a value from 0 to 255.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
AppendCountedString
-- num_count_bytes is an integer
-- big_endian is a boolean
-- str is a string
-- charset is a string
-- ok is true, or nil and err on failure
local ok, err = bd:append_counted_string(num_count_bytes, big_endian, str, charset)
Introduced in version 9.5.0.91

Appends a byte count followed by the string in the desired character encoding, such as utf-8. The num_count_bytes is the size in bytes of the integer to represent the byte count. If 0, then a minimal number of bytes is used. If big_endian is true, the byte count is appended in big endian byte ordering, otherwise little-ending byte ordering. The str is the string to be appended. The charset is the character encoding, such as utf-8, utf-16, windows-1252, etc.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
AppendData
-- data is a string of bytes
-- ok is true, or nil and err on failure
local ok, err = bd:append_data(data)
Introduced in version 11.0.0

Appends binary data to the current contents.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
AppendEncoded
-- enc_data is a string
-- encoding is a string
-- ok is true, or nil and err on failure
local ok, err = bd:append_encoded(enc_data, encoding)
Introduced in version 9.5.0.51

Appends encoded binary data to the current data. The encoding may be Base64, modBase64, base64Url, Base32, Base58, QP (for quoted-printable), URL (for url-encoding), Hex, or any of the encodings found at Chilkat Binary Encodings List.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
AppendEncodedSb
-- sb is a chilkat.StringBuilder
-- encoding is a string
-- ok is true, or nil and err on failure
local ok, err = bd:append_encoded_sb(sb, encoding)
Introduced in version 9.5.0.62

Decodes the contents of sb and appends the decoded bytes to this object. The encoding may be Base64, modBase64, base64Url, Base32, Base58, QP (for quoted-printable), URL (for url-encoding), Hex, or any of the encodings found at Chilkat Binary Encodings List.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
AppendInt2
-- value is an integer
-- little_endian is a boolean
-- ok is true, or nil and err on failure
local ok, err = bd:append_int2(value, little_endian)
Introduced in version 9.5.0.77

Appends a 16-bit integer (2 bytes). If little_endian is true, then the integer bytes are appended in little-endian byte order, otherwise big-endian byte order is used.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
AppendInt4
-- value is an integer
-- little_endian is a boolean
-- ok is true, or nil and err on failure
local ok, err = bd:append_int4(value, little_endian)
Introduced in version 9.5.0.77

Appends a 32-bit integer (4 bytes). If little_endian is true, then the integer bytes are appended in little-endian byte order, otherwise big-endian byte order is used.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
AppendPadded
-- str is a string
-- charset is a string
-- pad_with_space is a boolean
-- field_len is an integer
-- ok is true, or nil and err on failure
local ok, err = bd:append_padded(str, charset, pad_with_space, field_len)
Introduced in version 9.5.0.80

Appends a string to this object, padded to the field_len with NULL or SPACE chars. If pad_with_space is true, then SPACE chars are used and the string is not null-terminated. If field_len is false, then null bytes are used. The charset controls the byte representation to use, such as utf-8.

Note: This call will always append a total number of bytes equal to field_len. If the str is longer than field_len, the method returns false to indicate failure and nothing is appended.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
AppendSb
-- sb is a chilkat.StringBuilder
-- charset is a string
-- ok is true, or nil and err on failure
local ok, err = bd:append_sb(sb, charset)
Introduced in version 9.5.0.62

Appends the contents of a StringBuilder to this object.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
AppendString
-- str is a string
-- charset is a string
-- ok is true, or nil and err on failure
local ok, err = bd:append_string(str, charset)
Introduced in version 9.5.0.62

Appends a string to this object. (This does not append the BOM. If a BOM is required, the AppendBom method can be called to append the appropriate BOM.)

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
ByteSwap
-- size is an integer
bd:byte_swap(size)
Introduced in version 11.0.0

Does either 16-bit or 32-bit byte swapping of the entire contents. If size equals 16, then swaps in 2 bytes (e.g., 0x1234 → 0x3412). If size equals 32, then swaps 4 bytes (e.g., 0x12345678 → 0x78563412)

top
CharsetConvert
-- from_charset is a string
-- to_charset is a string
-- all_or_none is a boolean
-- ok is true, or nil and err on failure
local ok, err = bd:charset_convert(from_charset, to_charset, all_or_none)
Introduced in version 11.0.0

This method converts the character encoding of an object's contents from one format to another, such as from Windows-1252 to UTF-8. The all_or_none parameter determines error handling for unrepresentable bytes. If all_or_none is true, the object's contents remain unchanged when errors occur. If false, error bytes are discarded. The method returns true if successful and false if errors occur.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
Clear
-- ok is true, or nil and err on failure
local ok, err = bd:clear()
Introduced in version 9.5.0.51

Clears the contents.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
ContentsEqual
-- bin_data is a chilkat.BinData
-- flag is true or false
local flag = bd:contents_equal(bin_data)
Introduced in version 9.5.0.62

Return true if the contents of this object equals the contents of bin_data.

Returns true or false: an answer, not a success flag.

top
FindString
-- str is a string
-- start_idx is an integer
-- charset is a string
-- n is an integer
local n = bd:find_string(str, start_idx, charset)
Introduced in version 9.5.0.85

Return the index where the first occurrence of str is found. Return -1 if not found. The start_idx indicates the byte index where the search begins. The charset specifies the byte representation of str that is to be searched. For example, it can be utf-8, windows-1252, ansi, utf-16, etc.

top
GetByte
-- index is an integer
-- n is an integer
local n = bd:get_byte(index)
Introduced in version 9.5.0.92

Returns the value of the byte at the given index. The returned value is an integer from 0 to 255.

top
GetData
-- bytes is a string of bytes
local bytes = bd:get_data()
Introduced in version 11.0.0

Retrieves the binary data contained within the object.

Returns the bytes as a Lua string (empty if there is no data).

top
GetDataChunk
-- offset is an integer
-- num_bytes is an integer
-- bytes is a string of bytes
local bytes = bd:get_data_chunk(offset, num_bytes)
Introduced in version 11.0.0

Retrieves a chunk of the binary data contained within the object.

Returns the bytes as a Lua string (empty if there is no data).

top
GetEncoded
-- encoding is a string
-- str is a string, or nil and err on failure
local str, err = bd:get_encoded(encoding)
Introduced in version 9.5.0.51

Retrieves the binary data as an encoded string. The encoding may be Base64, modBase64, base64Url, Base32, Base58, QP (for quoted-printable), URL (for url-encoding), Hex, or any of the encodings found at Chilkat Binary Encodings List.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
GetEncodedChunk
-- offset is an integer
-- num_bytes is an integer
-- encoding is a string
-- str is a string, or nil and err on failure
local str, err = bd:get_encoded_chunk(offset, num_bytes, encoding)
Introduced in version 9.5.0.51

Retrieves a chunk of the binary data and returns it in encoded form. The encoding may be Base64, modBase64, base64Url, Base32, Base58, QP (for quoted-printable), URL (for url-encoding), Hex, or any of the encodings found at Chilkat Binary Encodings List.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
GetEncodedSb
-- encoding is a string
-- sb is a chilkat.StringBuilder
-- ok is true, or nil and err on failure
local ok, err = bd:get_encoded_sb(encoding, sb)
Introduced in version 9.5.0.64

Writes the encoded data to a StringBuilder. The encoding may be Base64, modBase64, base64Url, Base32, Base58, QP (for quoted-printable), URL (for url-encoding), Hex, or any of the encodings found at Chilkat Binary Encodings List.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
GetHash
-- algorithm is a string
-- encoding is a string
-- str is a string, or nil and err on failure
local str, err = bd:get_hash(algorithm, encoding)
Introduced in version 9.5.0.91

Returns the hash of the contents of this object. The algorithm is the hash algorithm, and can be sha1, sha256, sha384, sha512, sha3-224, sha3-256, sha3-384, sha3-512, md2, md5, ripemd128, ripemd160,ripemd256, or ripemd320. The encoding can be base64, modBase64, base64Url, base32, base58, qp (for quoted-printable), url (for url-encoding), hex, hexLower, or any of the encodings found at Chilkat Binary Encodings List.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
GetInt2
-- index is an integer
-- little_endian is a boolean
-- n is an integer
local n = bd:get_int2(index, little_endian)
Introduced in version 9.5.0.88

Returns the value of the 16-bit signed integer stored in big-endian or little-endian byte ordering at the given index.

top
GetInt4
-- index is an integer
-- little_endian is a boolean
-- n is an integer
local n = bd:get_int4(index, little_endian)
Introduced in version 9.5.0.88

Returns the value of the 32-bit signed integer stored in big-endian or little-endian byte ordering at the given index.

top
GetString
-- charset is a string
-- str is a string, or nil and err on failure
local str, err = bd:get_string(charset)
Introduced in version 9.5.0.67

Interprets the bytes according to charset and returns the string. The charset can be utf-8, utf-16, ansi, iso-8859-*, windows-125*, or any of the supported character encodings listed in the link below.

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
GetTextChunk
-- start_idx is an integer
-- num_bytes is an integer
-- charset is a string
-- str is a string, or nil and err on failure
local str, err = bd:get_text_chunk(start_idx, num_bytes, charset)
Introduced in version 9.5.0.85

Returns num_bytes bytes starting at start_idx. The bytes are interpreted according to charset (for example, utf-8, ansi, utf-16, windows-1252, etc.)

Returns the string, or nil, err on failure (or when nothing is found). The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
GetUInt2
-- index is an integer
-- little_endian is a boolean
-- n is an integer
local n = bd:get_uint2(index, little_endian)
Introduced in version 9.5.0.88

Returns the value of the 16-bit unsigned integer stored in big-endian or little-endian byte ordering at the given index.

top
GetUInt4
-- index is an integer
-- little_endian is a boolean
-- n is an integer
local n = bd:get_uint4(index, little_endian)
Introduced in version 9.5.0.88

Returns the value of the 32-bit unsigned integer stored in big-endian or little-endian byte ordering at the given index.

top
IsValidUtf8
-- flag is true or false
local flag = bd:is_valid_utf8()
Introduced in version 10.1.0

Returns true if this object contains valid utf-8 byte sequences. Any invalid byte sequence that is not valid utf-8 will cause this function to return false.

Returns true or false: an answer, not a success flag.

top
LoadEncoded
-- enc_data is a string
-- encoding is a string
-- ok is true, or nil and err on failure
local ok, err = bd:load_encoded(enc_data, encoding)
Introduced in version 9.5.0.51

Loads binary data from an encoded string, replacing the current contents, if any. The encoding may be Base64, modBase64, base64Url, Base32, Base58, QP (for quoted-printable), URL (for url-encoding), Hex, or any of the encodings found at Chilkat Binary Encodings List.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
LoadFile
-- path is a string
-- ok is true, or nil and err on failure
local ok, err = bd:load_file(path)
Introduced in version 9.5.0.51

Loads data from a file.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
RemoveByteVal
-- value is an integer
bd:remove_byte_val(value)
Introduced in version 9.5.0.90

Removes bytes having a specified value (0-255). For example, to remove all null bytes, pass 0 in value.

top
RemoveChunk
-- offset is an integer
-- num_bytes is an integer
-- ok is true, or nil and err on failure
local ok, err = bd:remove_chunk(offset, num_bytes)
Introduced in version 9.5.0.51

Removes a chunk of bytes from the binary data.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

More Information and Examples
top
SecureClear
-- ok is true, or nil and err on failure
local ok, err = bd:secure_clear()
Introduced in version 9.5.0.67

Securely clears the contents by writing 0 bytes to the memory prior to deallocating the internal memory.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
WriteAppendFile
-- path is a string
-- ok is true, or nil and err on failure
local ok, err = bd:write_append_file(path)
Introduced in version 9.5.0.91

Appends the contents of this object to a file.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top
WriteFile
-- path is a string
-- ok is true, or nil and err on failure
local ok, err = bd:write_file(path)
Introduced in version 9.5.0.62

Writes the contents to a file.

Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.

top