将字符串从6位二进制解码为8位二进制 我希望基于我的C++片段编写一个等价的R函数。见下文:

将字符串从6位二进制解码为8位二进制 我希望基于我的C++片段编写一个等价的R函数。见下文:,r,decode,rcpp,R,Decode,Rcpp,基本上,我想解码这个: 我`@@B@@@@@@@@@@@@@@@@@@@@@IGZJPCoA@@@B@@@y}wqCLnLp@@@@@@z@SvA@@@q^I|VeUt@@@ 消息生成时,通过向每个字符添加0x40,一次将6位转换为可打印字符。下面的代码描述了将可打印值转换回二进制的过程。字符串从可打印转换回二进制后,必须使用反向endian转换对其重新排序 致: 0010 0100 0000 1000 0000 0000 0010 0000 0000 0000 0000 0000 0000 0

基本上,我想解码这个:

我`@@B@@@@@@@@@@@@@@@@@@@@@IGZJPCoA@@@B@@@y}wqCLnLp@@@@@@z@SvA@@@q^I|VeUt@@@

消息生成时,通过向每个字符添加0x40,一次将6位转换为可打印字符。下面的代码描述了将可打印值转换回二进制的过程。字符串从可打印转换回二进制后,必须使用反向endian转换对其重新排序

致:

0010 0100 0000 1000 0000 0000 0010 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 00000011 0010 1110 0011 0011 0000 0000 0000 0000 0000 1110 1000 0000 0100 1111 0110 0000 0100 0000 0000 0000 11000 0101 1110 0010 0111 1100 0101 1010 0101 0101 0111 0100 0000 0000 0000

与此等效的R:

/*****************************************************************************/
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);
    }
}
/****************************************************************************
/*****************************************************************************/
无效二进制解码位(字符*输入字符串,无符号字符*输出字符串)
{
int i,j;
/*将字符串从6位二进制解码为8位二进制*/
/*将每4个单词组转换为3个单词*/
对于(i=0,j=0;i4);
out_字符串[j++]=(in_字符串[i+1]&0x0f)>2);
OutsStord[j++] =((iNixSt[i+Sc] 2)和0x03)P>您可以直接使用从RCPP导出的函数: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@@@")
*/

请注意,我在输入字符串的末尾添加了一个额外的,
@
,以获得所需的大小。我没有详细比较结果,但对于示例,我比较了您的二进制表示法与十六进制表示法。那么您的问题是什么?到目前为止您尝试了什么?我需要将字符串转换为二进制v我对编码/解码的数据只是略知一二。我真的在寻找如何实现这一点的方向。太好了!这就是我想要做的,也是一个很好的开始。额外的“@”应该不会引起问题。现在我需要弄清楚如何使用反向endian转换。
> 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