英文:
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 <- "\n"
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直接将控制字符存储为原始字节。也就是说,'\n'
在内部存储为 0xA0
。正如SamR在评论中建议的那样,您可以通过运行 charToRaw()
来验证这一点,它会显示原始字节缓冲区:
charToRaw(''\n'')
# [1] 0a
在这方面,它与基本上所有其他主流编程语言相似。
但与许多其他语言解释器一样,当在 REPL 终端上打印字符串值时,R会特殊处理它们。这就是为什么打印值 '\n'
会显示 "\n"
而不是换行符。如果您想显示字符串的值,则不能使用 print()
。1 相反,您需要使用 cat()
、writeLines()
或者如果要打印到标准错误流,则使用 message()
。
1 除非您将值包装在 noquote()
中,但通常更喜欢使用适当的文本输出函数。
英文:
In a nutshell, R directly stores control characters as raw bytes. That is, '\n'
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('\n')
# [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 '\n'
displays "\n"
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论