我怎样才能转换\xef\xbc\xa1b\xef\xbc\x99\xef\xbc\x98\xef\xbc\x97\xef\xbc\x96\xef\xbc\x95\xef\xbc\x94\xef\xbc\x93\xef\xbc\x92\xef\xbc\x91\xef\xbc\x90成Ab9876543210?有没有通过正则表达式解决的办法?
test <- dput("\xef\xbc\xa1b\xef\xbc\x99\xef\xbc\x98\xef\xbc\x97\xef\xbc\x96\xef\xbc\x95\xef\xbc\x94\xef\xbc\x93\xef\xbc\x92\xef\xbc\x91\xef\xbc\x90")
免责声明:以下内容在我的机器上有效,但由于我无法纯粹根据提供的示例复制您的全角字符串,这是根据我的问题版本的最佳猜测(将字符串粘贴到文本文件中,保存使用 UTF-8 编码,并使用指定为 UTF-8 的编码加载它。
\n\n步骤1。阅读正文(我添加了半角版本进行比较):
\n\n> test <- readLines("fullwidth.txt", encoding = "UTF-8")\n> test\n[1] "\xef\xbb\xbf\xef\xbc\xa1b\xef\xbc\x99\xef\xbc\x98\xef\xbc\x97\xef\xbc\x96\xef\xbc\x95\xef\xbc\x94\xef\xbc\x93\xef\xbc\x92\xef\xbc\x91\xef\xbc\x90" "Ab9876543210"\nRun Code Online (Sandbox Code Playgroud)\n\n第2步。验证全角和半角版本不相等:
\n\n# using all.equal()\ntest1 <- test[1]\ntest2 <- test[2]\n> all.equal(test1, test2)\n[1] "1 string mismatch"\n\n# compare raw bytes\n> charToRaw(test1)\n [1] ef bb bf ef bc a1 62 ef bc 99 ef bc 98 ef bc 97 ef bc 96 ef bc 95 ef\n[24] bc 94 ef bc 93 ef bc 92 ef bc 91 ef bc 90\n> charToRaw(test2)\n [1] 41 62 39 38 37 36 35 34 33 32 31 30\nRun Code Online (Sandbox Code Playgroud)\n\n对于任何感兴趣的人,如果将原始字节版本作为十六进制输入粘贴到utf-8 解码器中,您将看到除了字母 b(从第 7 个字节中的 62 映射)之外,其余字母均由 3 组成- 字节序列。此外,第一个 3 字节序列映射到“零宽度无中断空格字符”,因此当您将字符串打印到控制台时它不可见。
\n\n步骤3。使用包从全角转换为半角Nippon:
library(Nippon)\ntest1.converted <- zen2han(test1)\n\n> test1.converted\n[1] "\xef\xbb\xbfAb9876543210"\n\n# If you want to compare against the original test2 string, remove the zero \n# width character in front\n> all.equal(substring(test1.converted, 2), test2)\n[1] TRUE\nRun Code Online (Sandbox Code Playgroud)\n