在R中ASCII控制码的内存表示

huangapple go评论71阅读模式
英文:

Memory representation of ASCII control codes in R

问题

在R中,如果我写a <- "\n",R会在内存中存储什么?

它会存储0x0A(换行符),还是会存储0x5C6E(字面的\n)?

换句话说,是cat返回字符串在内存中的样子,而print将控制字符还原为转义序列(与上面的第一种编码一致),还是相反,即print返回字符串在内存中的样子,而cat解释转义序列(与上面的第二种编码一致)?

我觉得这令人困惑,因为在C中,\n只是一种快捷方式来写入控制代码。所以在C中,会在内存中写入换行符0x0A。我不认为你会从C的字符串中得到"\n"

英文:

If I write a &lt;- &quot;\n&quot; in R, what does R store in memory?

Does it store 0x0A (line feed character), or does it store 0x5C6E (literal \n)?

Said otherwise, is cat that returns the string as it appears in memory, and print that reverts back control codes into escape sequences (consistently with first encoding above), or the other way around, that is, print that returns the string as it is in memory, and cat that interprets escape sequences (consistently with second encoding above)?

I find it confusing because in C "\n" was only a shortcut to write the control codes. So C would write the line feed character 0x0A in memory. I don't think you would ever get "\n" back from a string in C.

答案1

得分: 4

总之,R直接将控制字符存储为原始字节。也就是说,&#39;\n&#39; 在内部存储为 0xA0。正如SamR在评论中建议的那样,您可以通过运行 charToRaw() 来验证这一点,它会显示原始字节缓冲区:

charToRaw('&#39;\n&#39;')
# [1] 0a

在这方面,它与基本上所有其他主流编程语言相似。

但与许多其他语言解释器一样,当在 REPL 终端上打印字符串值时,R会特殊处理它们。这就是为什么打印值 &#39;\n&#39; 会显示 &quot;\n&quot; 而不是换行符。如果您想显示字符串的,则不能使用 print()1 相反,您需要使用 cat()writeLines() 或者如果要打印到标准错误流,则使用 message()


1 除非您将值包装在 noquote() 中,但通常更喜欢使用适当的文本输出函数。

英文:

In a nutshell, R directly stores control characters as raw bytes. That is, &#39;\n&#39; is stored internally as 0xA0. As suggested by SamR in the comment, you can verify this by running charToRaw(), which shows you the raw byte buffer:

charToRaw(&#39;\n&#39;)
# [1] 0a

In this regard it mirrors essentially every other mainstream programming language.

But like many other language interpreters, R treats string values specially when printing them on the REPL terminal. That’s why printing the value &#39;\n&#39; displays &quot;\n&quot; instead of a line break. If you want to display the value of strings, you therefore can’t use print().<sup>1</sup> Instead, you need to use either cat(), writeLines() or, if you want to print to the standard error stream, message().


<sup>1</sup> Unless you wrap the value in noquote(), but using a proper text output function is generally preferred.

huangapple
  • 本文由 发表于 2023年7月20日 22:18:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定