您可以直接从Rcpp通过R导出的函数使用C++(井C)代码:
#include <Rcpp.h>
void Binary_Decode_6bit(char *in_string, unsigned char *out_string)
{
int i,j;
/* DECODE string from 6 bit binary to 8 bit binary */
/* Convert each 4 word group into 3 words */
for (i=0, j = 0; i < strlen(in_string); i += 4)
{
out_string[j++] = ((in_string[i] &0x3f) << 2) | ((in_string[i+1] &0x30) >> 4);
out_string[j++] = ((in_string[i+1] &0x0f) << 4) | ((in_string[i+2] &0x3c) >> 2);
out_string[j++] = ((in_string[i+2] &0x03) << 6) | (in_string[i+3] &0x3f);
}
}
// [[Rcpp::export]]
Rcpp::RawVector decode(std::string input) {
if (input.size() % 4 != 0)
Rcpp::stop("input size must be a multiple of 4");
std::vector<unsigned char> tmp(input.size() * 3 / 4);
Binary_Decode_6bit(&input[0], &tmp[0]);
Rcpp::RawVector result(tmp.size());
std::copy(tmp.begin(), tmp.end(), result.begin());
return result;
}
/*** R
decode("I@`@@B@@@@@@@@@@@@@@@@@@@IGZJPCoA@@@@B@@|y}wqCLnLp@@@@@@z@SvA@@@q^I|VeUt@@@@")
decode("I@`@@B@@@@@@@@@@@@@@@@@@@IGZJPCoA@@@@B@@|y}wqCLnLp@@@@@@z@SvA@@@q^I|VeUt@@@")
*/
输出:
> decode("I@`@@B@@@@@@@@@@@@@@@@@@@IGZJPCoA@@@@B@@|y}wqCLnLp@@@@@@z@SvA@@@q^I|VeUt@@@@")
[1] 24 08 00 00 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 91 da 29 00 ef 04 00 00 00 20 00 f3 9f 77 c4 33
[36] 2e 33 00 00 00 00 00 e8 04 f6 04 00 00 c5 e2 7c 5a 55 74 00 00 00
> decode("I@`@@B@@@@@@@@@@@@@@@@@@@IGZJPCoA@@@@B@@|y}wqCLnLp@@@@@@z@SvA@@@q^I|VeUt@@@")
Error in decode("I@`@@B@@@@@@@@@@@@@@@@@@@IGZJPCoA@@@@B@@|y}wqCLnLp@@@@@@z@SvA@@@q^I|VeUt@@@") :
input size must be a multiple of 4
@
在输入字符串的末尾获取所需的大小。我没有详细比较结果,但是对于我比较过的示例,你的二进制表示和我的十六进制表示是一样的。