What is embedded zero?

I am reading the Lua reference manual, and it talks about "built-in zeros", symbolized by "\ 0".

When I try to see it in the Lua console, it does not print anything meaningful:

> print "a \0 b" a 

So what is this "embedded zero"?

+7
source share
4 answers

Each character has an internal numeric representation, such as \ 97 for 'a'. The character code \ 0 does not represent any visible character, but is used as a terminator in C and other programming languages.

The manual wants to make it clear that "\ 0" is not a terminator in Lua. It also means that you can load arbitrary bytes into a string (image, audio, video, native code, etc.), and you don’t risk truncating it with the first '\ 0' some library function (which can happen in C if you use string related functions).

+14
source

\0 is just a byte with a null value, it doesn't need any fancy name. Lua strings are just byte strings that track their length, so they can contain any byte value, including zero. Some functions process these byte strings as if they were C strings ending with \0 , apparently print does this.

This means that in lua #s (string length) O (1) versus O (n) for C strings. And the application can use lua strings for any byte streams, for example, UTF-16 encoded text or binary file contents.

+3
source

It will look like sending a NULL character to a C string. Although your print output does not display a b character, other Lua functions should work with the full string length (unlike C string processing functions that work with NULL terminated strings).

One of them is to use one line to store several values ​​separated by \0 .

+1
source

A null character is often represented as an escape sequence of \ 0 in the source text of strings or character constants.

Wikipedia Zero character

0
source

All Articles