.byte()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Sep 10, 2023
Contribute to Docs

The string.byte() function takes a character or a string as an argument and returns an ASCII (decimal) equivalent internal numeric representation.

Syntax

string.byte(s, n)

The function can take two arguments, where the s parameter is a character or string and n is the index of the string passed as an argument. Passing only one argument will set n to 1.

Example 1

The example below demonstrates the use of the .byte() method.

print(string.byte('a'))
print(string.byte('9'))
print(string.byte(9))
print(string.byte(' '))
print(string.byte(';'))

This will return the following output:

97
57
57
32
59

Example 2

This example demonstrates the use of .byte with a second argument:

print(string.byte('abca', 4))
print(string.byte(998877, 1))
print(string.byte(998877, 3))
print(string.byte(998877, 5))

This will return the following results.

97
57
56
55

All contributors

Looking to contribute?

Learn Lua on Codecademy