string.byte()
The string.byte()
function retrieves the numeric ASCII (or Unicode) value of a character or a sequence of characters in a string. It is commonly used for encoding, debugging, and understanding character-level data.
Syntax
string.byte(s, start, end_)
Parameters
- Name
s
- Type
- string
- Description
The string from which the ASCII (or Unicode) values of characters will be extracted.
- Name
start
- Type
- number (default: 1)
- Description
The starting position in the string to begin retrieving character values.
- Name
end_
- Type
- number (optional)
- Description
The ending position in the string up to which character values will be extracted. If omitted, only the value at the
start
position will be returned.
Return Value
The function returns:
- A single numeric value if only
start
is provided. - Multiple numeric values if both
start
andend_
are provided.
Examples
Example 1: Retrieve a single ASCII value
The function retrieves the ASCII (or Unicode) value of the first character when no start
is specified.
Example 1
print(string.byte("Lua"))
-- Output: 76 (ASCII for 'L')
Example 2: Retrieve values for a range of characters
The function retrieves values for all characters between the specified start
and end_
positions.
Example 2
local result = {string.byte("Hello, Lua!", 1, 5)}
print(table.concat(result, ", "))
-- Output: 72, 101, 108, 108, 111
Example 3: ASCII for special characters
You can extract numeric values for special characters as well.
Example 3
print(string.byte("!")) -- Output: 33
print(string.byte("$")) -- Output: 36
print(string.byte("@")) -- Output: 64
Example 4: Extracting the last character's value
By specifying only the start
parameter, you can extract the ASCII value of the last character in a string.
Example 4
local str = "Lua is great!"
print(string.byte(str, #str))
-- Output: 33 (ASCII for '!')
Example 5: Decoding a sequence of characters
You can combine string.byte()
with a loop to decode every character in a string into its ASCII values.
Example 5
local str = "Code"
for i = 1, #str do
print(string.byte(str, i))
end
-- Output:
-- 67 (C)
-- 111 (o)
-- 100 (d)
-- 101 (e)
Example 6: Invalid parameters
When you pass invalid parameters, the function handles them gracefully by returning nil
.
Example 6
print(string.byte("", 1)) -- Output: nil (empty string)
print(string.byte("Hello", 10)) -- Output: nil (out of range)
Use Cases
- Encoding: Convert strings into numeric values for processing or communication.
- Debugging: Analyze strings to understand encoding or detect invalid characters.
- Cryptography: Transform characters into numeric representations for algorithms.
See also
string.char()
: Converts numeric ASCII (or Unicode) values back into their corresponding characters. This is essentially the reverse ofstring.byte()
.string.sub()
: Extracts substrings, which can be combined withstring.byte()
to analyze or process specific sections of a string.string.unpack()
: Retrieves structured data from a packed string, which may complementstring.byte()
when dealing with encoded strings.