Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将十进制数组(str)转换为二进制数组(字节)_C - Fatal编程技术网

C 将十进制数组(str)转换为二进制数组(字节)

C 将十进制数组(str)转换为二进制数组(字节),c,C,请提供一些代码,用于将十进制值的char[]数组转换为c中大整数值的字节数组 如何将下面的代码转换为十进制值的大数组,例如,我将结果转换为长字节数组 static int dec2bin (char inbuf[], int num_convert) { char ctemp; int result, power; num_convert--; /* index of LS char to convert */ result = 0; power = 1; while

请提供一些代码,用于将十进制值的char[]数组转换为c中大整数值的字节数组

如何将下面的代码转换为十进制值的大数组,例如,我将结果转换为长字节数组

static int dec2bin (char inbuf[], int num_convert)
{
  char ctemp;
  int result, power;

  num_convert--; /* index of LS char to convert */
  result = 0;
  power = 1;
  while (num_convert >= 0)
  {
    ctemp = inbuf[num_convert--]; /* working digit */
    if (isdigit(ctemp))
    {
      result += ((ctemp-'0') * power);
      power *= 10;
    }
    else
      return(0); /* error: non-numeric digit detected */
  }
  return (result);
}
不,它不仅仅是长值,它实际上是一个大整数值,谁能给出一个简单的十进制到字节(二进制转换逻辑,我将用我的bigint实现和bigint运算符(add,mult)等替换int

塞缪尔是对的

提前谢谢

例如,我可以按如下方式替换上述内容

static int dec2bin (char inbuf[], bigint num_convert) 
{ 
  char ctemp; 
  bigint result, power; 

  num_convert--; /* index of LS char to convert */ 
  result = 0; 
  power = 1; 
  while (num_convert >= 0) 
  { 
    ctemp = inbuf[num_convert--]; /* working digit */ 
    if (isdigit(ctemp)) 
    { 
      result = bi_add(result ,(bi_mult((ctemp-'0'), power)); 
      power = bi_mult(power , 10); 
    } 
    else 
      return(0); /* error: non-numeric digit detected */ 
  } 
  return (result); 
} 

像这样的东西会有用吗?

听起来你在寻找一个解决方案,你自己做低级算术

您是否考虑过使用现有的bignum包,如?一旦拥有了它,转换现有代码就相当容易了。例如,以下代码:

result += ((ctemp-'0') * power);
power *= 10;
变成:

mpz_t tmp;
mpz_init(tmp);
mpz_mul_ui(tmp, power, ctemp - '0');
mpz_add(result, result, tmp);
mpz_clear(tmp);

mpz_mul_ui(power, power, 10);

我认为“大”不是指数字的大小,而是指数字字符串数组的大小。我知道OP想要的数字范围是
long
@Carl-注意OP写的“我得到的结果是长字节数组”既然他要的是一系列的多头,听起来他们肯定是在寻找一个比多头更大的东西。嗯。我同意可能是我误解了这个问题,你的解释也是合理的。