string.char() function in Lua programming


There are so many scenarios where you might want to convert a decimal value into a character representation. The character representation of a decimal or integer value is nothing but the character value, which one can interpret using the ASCII table.

In Lua, to convert a decimal value to its internal character value we make use of the string.char() function.

Syntax

string.char(I)

In the above syntax, the identifier I represents the decimal value which we want to convert into a character.

Example

Let’s consider a very simple example, where you are given different decimal values, and you want to convert them to a character value.

Consider the example shown below −

 Live Demo

s = string.char(97)
print(s)
s = string.char(122)
print(s)
s = string.char(125)
print(s)

In the above example, the values passed as argument to the string.char() function are decimal values that correspond to a character.

Output

a
z
}

The string.char() function can take multiple arguments as well.

Example

Consider the example shown below −

 Live Demo

i = 97
s = string.char(i,i+1,i+2,i+3)
print(s)

Output

abcd

Updated on: 19-Jul-2021

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements