Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.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 为什么点十进制中的最后一个数字不转换为二进制?_C - Fatal编程技术网

C 为什么点十进制中的最后一个数字不转换为二进制?

C 为什么点十进制中的最后一个数字不转换为二进制?,c,C,我想把ip地址转换成二进制,但是, 它总是将0作为最后一个十进制数的二进制返回。 为什么它不执行separate4()函数?注释中已经列出了您做错的事情,但您做的事情过于复杂,几乎像鲁布·戈德伯格一样。它非常简单,不需要任何复杂的技巧就可以完成 Enter dotted decimal ip address : 192.15.7.4 Binary Format of IP : 11000000.1111.111.0 #包括 #包括 #包括 //所有支票都兑现了! char*int8_到_-b

我想把ip地址转换成二进制,但是, 它总是将0作为最后一个十进制数的二进制返回。
为什么它不执行
separate4()
函数?

注释中已经列出了您做错的事情,但您做的事情过于复杂,几乎像鲁布·戈德伯格一样。它非常简单,不需要任何复杂的技巧就可以完成

Enter dotted decimal ip address :

192.15.7.4
Binary Format of IP :
11000000.1111.111.0
#包括
#包括
#包括
//所有支票都兑现了!
char*int8_到_-bin(intn,char*buf)
{
int i=8;
//我们需要一个无符号整数来进行位杂耍
//因为二的补码。
//这里不需要,因为输入是正的
无符号整数N;
//检查尺寸是否合适
如果(n<0 | | n>255){
返回NULL;
}
//现在安全了
N=(无符号整数)N;
//我们在这里向后工作,首先是最低有效位
//但我们希望它的最低有效位是最后一位。
而(我--){
//从整数中提取一个(字符)数字
buf[i]=(字符)(N&0x1)+“0”;
//向右移动一位,然后
//通过这样做来删除最低有效位
N>>=1;
}
//将指针返回到我们得到的缓冲区(以便于使用)
返回buf;
}
int main(int argc,字符**argv)
{
//保持中间整数
国际地址;
//命令行参数,strtol()和int8_to_bin()的助手
字符*s,*endptr,*cp;
//int8_to_bin()要使用的缓冲区
//初始化为所有“\0”;
char buf[9]={'\0'};
//保存最终结果的数组
//四个8字符组,有三个句点和一个NUL
charbinaddr[4*8+3+1];
//迭代器
int i;
如果(argc<2){
fprintf(stderr,“用法:%s\u ipv4\n”,argv[0]);
退出(退出失败);
}
//将指向第一个参数的指针设置为快捷方式
s=argv[1];
//当然,以下操作可以在单个循环中完成
//strtol()跳过前导空格并最多解析第一个非数字。
//endptr指向输入中由strtol()决定的点
//它必须是第一个非数字
地址=(int)strtol(s,&endptr,0);
//在复制时检查NULL,同时保留buf的内容
//(此处未完成检查)
cp=int8至bin(地址,buf);
//如果(cp==NULL)。。。
//将结果复制到结果数组
//cp有一个NUL,是一个正确的C字符串
strcpy(binaddr,cp);
//冲洗并重复三次
对于(i=1;i<4;i++){
//跳过数字和句点之间的任何内容,
//(或使用strhr()执行此操作)
而(*endptr!='。){
endptr++;
}
//跳过句号本身
endptr++;
//在输出中添加句点
strcat(binaddr,“.”);
//下一个号码
addr=(int)strtol(endptr和endptr,0);
cp=int8至bin(地址,buf);
//如果(cp==NULL)。。。
strcat(binadrr,cp);
}
printf(“输入:%s\n”,s);
printf(“输出:%s\n”,binaddr);
退出(退出成功);
}
我们不需要复杂的解析算法,
strtol()
为我们做这件事,我们只需要自己找到下一个周期。所有输入和输出的大小都是已知的,并且/或者如果在其限制范围内,则可以轻松地进行检查——例如:不需要繁琐且容易出错的内存分配,我们可以使用固定大小的缓冲区和
strcat()


有一个原则不仅适用于海军,也适用于编程:。

为什么不使用像
inet\u addr()
这样的库函数?为什么需要4个函数?使用
strtok()
将字符串拆分并在循环中处理。不相关的
bin\u verify
是无意义的。
separate4()
查找
,然后将
之前的所有字符复制到
a
中。但是最后一个数字后面没有
;如果(scanf(“%d.%d.%d.%d”,n,n+1,n+2,n+3)==4)为您提供了一个整数数组。你需要做的就是把每一个都转换成二进制。
Enter dotted decimal ip address :

192.15.7.4
Binary Format of IP :
11000000.1111.111.0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// ALL CHECKS OMMITTED!

char *int8_to_bin(int n, char *buf)
{
  int i = 8;
  // we need an unsigned integer for the bit-juggling
  // because of two's complement numbers.
  // Not really needed here because the input is positive
  unsigned int N;
  // check for proper size
  if (n < 0 || n > 255) {
    return NULL;
  }
  // is safe now
  N = (unsigned int) n;
  // we work backwards here, least significant bit first
  // but we want it least significant bit last.
  while (i--) {
    // make a (character) digit out of an integer
    buf[i] = (char) (N & 0x1) + '0';
    // shift one bit to the right and
    // drop the least significant bit by doing it
    N >>= 1;
  }
  // return the pointer to the buffer we got (for easier use)
  return buf;
}

int main(int argc, char **argv)
{
  // keeps the intermediate integer
  int addr;
  // command-line argument, helper for strtol() and int8_to_bin()
  char *s, *endptr, *cp;
  // buffer for int8_to_bin() to work with
  // initialize to all '\0';
  char buf[9] = { '\0' };
  // array holding the end-result
  // four 8-character groups with three periods and one NUL
  char binaddr[4 * 8 + 3 + 1];
  // iterator
  int i;

  if (argc < 2) {
    fprintf(stderr, "Usage: %s dotted_ipv4\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  // set a pointer pointing to the first argument as a shortcut
  s = argv[1];

  // the following can be done in a single loop, of course

  // strtol() skips leading spaces and parses up to the first non-digit.
  // endptr points to that point in the input where strtol() decided
  // it to be the first non-digit
  addr = (int) strtol(s, &endptr, 0);

  // work on copy to check for NULL while keeping the content of buf
  // (checking not done here)
  cp = int8_to_bin(addr, buf);
  // if(cp == NULL)...

  // copy the result to the result-array
  // cp has a NUL, is a proper C-string
  strcpy(binaddr, cp);

  // rinse and repeat three times
  for (i = 1; i < 4; i++) {
    // skip anything between number and period,
    // (or use strchr() to do so)
    while (*endptr != '.'){
      endptr++;
    }
    // skip the period itself
    endptr++;
    // add a period to the output
    strcat(binaddr, ".");
    // get next number
    addr = (int) strtol(endptr, &endptr, 0);
    cp = int8_to_bin(addr, buf);
    // if(cp == NULL)...
    strcat(binaddr, cp);
  }
  printf("INPUT:  %s\n", s);
  printf("OUTPUT: %s\n", binaddr);

  exit(EXIT_SUCCESS);
}