In various languages, you can embed hex values in a string literal by using the \x escape sequence:
"hello \x77\x6f\x72\x6c\x64"
How can I do the same thing in Lua 5.1?
Since Lua 3.1, you can use decimal escapes in strings liberals:
print "hello \119\111\114\108\100"
Starting with Lua 5.2, you can use hex escapes in string literals:
print "hello \x77\x6f\x72\x6c\x64"
In Lua 5.1, you can convert hex escapes a posteriori:
s = [[hello \x77\x6f\x72\x6c\x64]]
s = s:gsub("\\x(%x%x)", function (x) return string.char(tonumber(x, 16)) end)
print(s)
Note the use of long strings, which do not interpret escape sequences. If you use short strings (in quotes) as in your original code, then \x
will be silently converted to x
, because Lua 5.1 does not understand \x
. Lua 5.2 and later complains about escape sequences that it does not understand.