将ascii数转换为R中的字符串

时间:2014-04-17 02:00:56

标签: r

要将字符串转换为R中的ascii代码,我通常使用:

> strtoi(charToRaw("abcd"),16L)
[1] 97 98 99 100

是否有反函数,即

>myDesiredFunc(c(97 98 99 100)) 
[1] "abcd"

感谢。

3 个答案:

答案 0 :(得分:7)

此外:

 rawToChar(as.raw(c(97,98,99,100)))

在命令提示符下键入?charToRaw以获取更多信息。

答案 1 :(得分:7)

我刚注意到R有一个intToUtf8utf8ToInt个函数可以执行相同的操作。

> test<-utf8ToInt("Apples")
> test
[1]  65 112 112 108 101 115
> intToUtf8(test)
[1] "Apples"

答案 2 :(得分:1)

我找到的最好的是:

readLines(rawConnection(as.raw(c(97,98,99,100,13))))

虽然我想最好关闭连接

con = rawConnection(as.raw(c(97,98,99,100,13)))
res = readLines(con);
close(con);
show(res);

[1] "abcd"