什么是嵌入零?

发布于 2024-12-21 06:30:29 字数 212 浏览 0 评论 0原文

我正在阅读Lua参考手册,它谈到了“嵌入零”,用“\0”表示。

当我尝试在 Lua 控制台中查看它时,它没有打印出任何有意义的内容:

> print "a \0 b"
a 

那么,这个“嵌入零”是什么?

I'm reading the Lua reference manual, and it talks about "embedded zeros", symbolized by "\0".

When I try to see it in the Lua console, it prints nothing meaningful:

> print "a \0 b"
a 

So, what's this "embedded zero"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

她比我温柔 2024-12-28 06:30:29

每个字符都有一个内部数字表示,例如 \97 代表“a”。代码为 \0 的字符不代表任何可见字符,但在 C 和其他编程语言中用作终止符。

该手册希望明确说明 '\0' 在 Lua 中不是终止符。这也意味着您可以将任意字节加载到字符串中(图像、音频、视频、本机代码等),并且您不会冒被某些库函数在第一个“\0”处截断的风险(这可能发生在 C 语言中)如果您使用字符串相关的函数)。

Every character has an internal numeric representation, such as \97 for 'a'. A character with 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 a '\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 do not risk having it truncated at the first '\0' by some library function (which could happen in C if you use string-related functions).

染柒℉ 2024-12-28 06:30:29

\0 只是一个值为零的字节,它不需要任何花哨的名称。 Lua 字符串只是记录其长度的字节字符串,因此它们可以包含任何字节值,包括零。某些函数将这些字节字符串视为以 \0 结尾的 C 字符串,显然 print 就是这样做的。

这意味着在 lua 中,#s(字符串长度)是 O(1),而 C 字符串则为 O(n)。应用程序可以将 lua 字符串用于任何字节流,例如 UTF-16 编码的文本或二进制文件内容。

\0 is just a byte with the value zero, it doesn't need any fancy name. Lua strings are just byte strings that keep track of their length, so they may contain any byte values, zero included. Some functions treat these byte strings as if they were C strings that terminate with \0, apparently print does this.

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

爱你不解释 2024-12-28 06:30:29

这与在 C 字符串中放入 NULL 字符类似。尽管打印输出不显示 b 字符,但其他 Lua 函数应该处理字符串的完整长度(与处理 NULL 终止字符串的 C 字符串处理函数不同)。

其用途之一是使用一个字符串来保存由 \0 分隔的多个值。

It is going to be similar to putting a NULL character in a C string. Although your print output does not show the b character, other Lua functions should work with the full length of the string (unlike C string handling functions that work with NULL terminated strings).

One use of this would be to use one string for holding multiple values separated by \0.

久伴你 2024-12-28 06:30:29

当 Lua 5.1 在字符串中存在 nul 字节的几个问题时,Lua 5.2 工作得很好。
我发现函数 print 会丢弃 nul 字节之后的所有字符。
另外,函数 string.format 返回第一个 nul 字符之前的字符串。

Lua 5.2 works well when Lua 5.1 has several problems with nul bytes in the string.
I've found that the function print dischards all characters after the nul byte.
Also the function string.format returns string up to the first nul character.

自此以后,行同陌路 2024-12-28 06:30:29

空字符通常表示为转义序列 \0
源代码字符串文字或字符常量。

维基百科空字符

The null character is often represented as the escape sequence \0 in
source code string literals or character constants.

Wikipedia Null Character

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文