R转换十六进制到base64

时间:2017-09-24 06:04:58

标签: r base64 hex

我正在尝试将十六进制转换为base64,但我使用base64Encode或base64_enc等函数进行的转换与我从此网站https://conv.darkbyte.ru/或此网站http://tomeko.net/online_tools/hex_to_base64.php?lang=en

library(RCurl)
library(jsonlite)

hex_number="9d0a5a7d6771dd7fa321a48a820f93627657df
3292548df1389533913a60328300a9cc80d982875a8d08bb7
602c59935cacae88ea635ed8d3cea9ef57b1884cc"

base64_enc(hex_number)
#"OWQwYTVhN2Q2NzcxZGQ3ZmEzMjFhNDhhODIwZjkzNjI3NjU3ZGYKMzI5M
#jU0OGRmMTM4OTUz\nMzkxM2E2MDMyODMwMGE5Y2M4MGQ5ODI4NzVhOGQwO
#GJiNwo2MDJjNTk5MzVjYWNhZTg4ZWE2\nMzVlZDhkM2NlYTllZjU3YjE4ODRjYw=="

base64Encode(hex_number)
#"OWQwYTVhN2Q2NzcxZGQ3ZmEzMjFhNDhhODIwZjkzNjI3NjU3ZGYKMzI5M
#jU0OGRmMTM4OTUzMzkxM2E2MDMyODMwMGE5Y2M4MGQ5ODI4NzVhOGQwOGJiNwo
#2MDJjNTk5MzVjYWNhZTg4ZWE2MzVlZDhkM2NlYTllZjU3YjE4ODRjYw=="

#desired result:
#nQpafWdx3X+jIaSKgg+TYnZX3zKSVI3xOJUzkTpgMoMAqcyA2YKHWo0Iu3YCxZk1ysrojqY17Y086p71exiEzA==

此外,我尝试将HEX改为文本,然后使用此页面中的代码将其更改为HEX http://blog.entropic-data.com/2017/04/19/short-dealing-with-embedded-nul-in-string-manipulation-with-r/我没有得到我想要的结果。

1 个答案:

答案 0 :(得分:2)

wkb包中借用一些代码(或者直接安装并使用它),将十六进制字符串转换为原始向量,然后将其传递给基本的64个转换例程之一:

hex_number <- "9d0a5a7d6771dd7fa321a48a820f93627657df3292548df1389533913a60328300a9cc80d982875a8d08bb7602c59935cacae88ea635ed8d3cea9ef57b1884cc"

我&#39;&#34; source - ing&#34;这个但你应该在本地复制代码,如果你计划使用它,因为GH可能会关闭或代码可能会改变。

source_url("https://raw.githubusercontent.com/ianmcook/wkb/master/R/hex2raw.R",
           sha1 = "4443c72fb3831e002359ad564f1f2a8ec5e45e0c")

openssl::base64_encode(hex2raw(hex_number))
## [1] "nQpafWdx3X+jIaSKgg+TYnZX3zKSVI3xOJUzkTpgMoMAqcyA2YKHWo0Iu3YCxZk1ysrojqY17Y086p71exiEzA=="

OR(如果您愿意将wkb包作为依赖项:

openssl::base64_encode(wkb::hex2raw(hex_number))