简体   繁体   中英

How can I save the output of for loop in terms of integer?

for (c = 8; c >= 0; c--){
    k = ip4 >> c;
    if (k & 1)
        printf("1");
    else
        printf("0");
}

Hello everyone.I'm trying to convert an ip adress from base10 or base 16 to binary.I wrote this code to convert ip adress to binary.But also I need to save the output of for loop so that answer such a question and continue code. Question is on th link.It is forbidden to use arrays and data structures.Here is my assignment that I'm trying to solve.I will be very happy if you solve my problem.在此处输入图片说明 Than you for your help. Here is my code from beginnig to for loop.在此处输入图片说明 I added my code from beginning so that you can see what (ip4) is.For example program will ask the user (Enter ip adress:) and user input such an ip adres (168.122.1.2) in that ip adress (168 is equal to ip1)(122 is equal ip2)(1 equal ip 3)(2 equan ip4).So I have 4 for loop to convert it binary each block.But I need to save the binary format to continue .My question is how can I save for example (00000001) for ip4.I need to save as an integer.

    #include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main (){

char ch;    
int base,ip1,ip2,ip3,ip4,ip;
int sub1,sub2,sub3,sub4,subnet;
printf("Please enter the Base for your IP Address (10/16):");
scanf("%d",&base);
if(base!=10 && base!=16)
    printf("Sorry that is not a valid base!\n");
if(base==10){
printf("Please enter the IP Address:");
scanf("%d%c%d%c%d%c%d",&ip1,&ch,&ip2,&ch,&ip3,&ch,&ip4);
ip=ip1+ip2+ip3+ip4;
if(ip>0&&ip<1020)
    printf("Thanks it is a valid IP address!\n");
else
    printf("This is NOT a valid IP Address!");  

printf("Please enter the Subnet Mask: ");
scanf("%d%c%d%c%d%c%d",&sub1,&ch,&sub2,&ch,&sub3,&ch,&sub4);
subnet=sub1+sub2+sub3+sub4;
if(subnet>0&&subnet<1020)
printf("Thanks it is a valid Subnet Mask!\n");}

else if(base==16){
printf("Please enter the IP Address:");
scanf("%d%c%d%c%d%c",&ip1,&ch,&ip2,&ch,&ip3,&ch,&ip4);
ip=ip1+ip2+ip3+ip4;
if(ip>0&&ip<1020)
    printf("Thanks it is a valid IP address!\n");
else
    printf("This is NOT a valid IP Address!");  
printf("Please enter the Subnet Mask: ");
scanf("%d%c%d%c%d%c%d",&sub1,&ch,&sub2,&ch,&sub3,&ch,&sub4);
subnet=sub1+sub2+sub3+sub4;
if(subnet>0&&subnet<1020)
printf("Thanks it is a valid Subnet Mask!\n");}         

int menu,k,c;
while(1){
printf("1. Convert IP address to binary\n");
printf("2. Convert subnet mask to binary\n");
printf("3. Specify the class of the IP address\n");
printf("4. Specify the number of host addresses availablen\n");
printf("5. Provide new IP Address and subnet mask\n");
printf("6. Exit\n");
scanf("%d",&menu);
while(menu>0 && menu<6){
    if(menu==1){
        for (c = 8; c >= 0; c--)
  {
    k = ip1 >> c;

    if (k & 1)
      printf("1");
    else
      printf("0");
  }
    printf(".");
  for (c = 8; c >= 0; c--)
  {
    k = ip2 >> c;

    if (k & 1)
      printf("1");
    else
      printf("0");
  }
    printf(".");
    for (c = 8; c >= 0; c--)
  {
    k = ip3 >> c;

    if (k & 1)
      printf("1");
    else
      printf("0");
  } 
    printf(".");
  for (c = 8; c >= 0; c--)
  {
    k = ip4 >> c;

    if (k & 1)
      printf("1");
    else
      printf("0");

  }
        printf("\n");
        break;

    }
    else if (menu==2){

    for (c = 8; c >= 0; c--)
  {
    k = sub1 >> c;

    if (k & 1)
      printf("1");
    else
      printf("0");
  }
    printf(".");
  for (c = 8; c >= 0; c--)
  {
    k = sub2 >> c;

    if (k & 1)
      printf("1");
    else
      printf("0");
  }
    printf(".");
    for (c = 8; c >= 0; c--)
  {
    k = sub3 >> c;

    if (k & 1)
      printf("1");
    else
      printf("0");
  } 
    printf(".");
  for (c = 8; c >= 0; c--)
  {
    k = sub4 >> c;

    if (k & 1)
      printf("1");
    else
      printf("0");

  }
        printf("\n");
        break;
}

}
}

    return 0;

}

You can test the code.But you should enter base 10 and ip will be just inlude point ant integer just like 192.168.1.1 also subnet is need to be like that.

You want to convert an IP address in binary form, but this is not what you've been asked to do.

The question you have could be reduced to "what are the first three bits of this 32bits integer?"

void start_of_ip(int32_t ip4) {

    /* testing if first bit is not set 
    0x80000000 is 1000 0000 0000 0000 0000 0000 0000 0000 */
    if (!(ip4 & 0x80000000)) {
        /* class A IP*/
        puts("start with b0")
    } else {    
        /* in this block, we know that first bit is set */

        /* testing if second bit is not set */          
        if (!(ip4 & 0x40000000)) {
            /* class B IP*/
            puts("start with b10");
        } else {                
            /* in this block, we know that first two bits are set */

            /* testing if third bit is not set */           
            if (!(ip4 & 0x20000000)) {
                /* class C IP*/
                puts("start with b110");
            } else {
                /* the rest */
                puts("other case");
            }               
        }
    }
}

I need to save as an integer.

You're lucky, since you already have saved your IP address parts ip1ip4 as integers.

But I need to save the binary format to continue .

Probably the integers are already stored in a plain binary representation, but the C standard guarantees that:

Values stored in … objects of type unsigned char shall be represented using a pure binary notation.

So, at least after storing an address part in an unsigned char , you can safely continue with step e. of your programming assignment without worrying about the textual representation of the address. To determine the address class according to the shown table, it suffices to shift and mask the first number:

        if (menu == 3)
        {
            unsigned char first = ip1;  // convert to "a pure binary notation"
            char class = first>>7 == 0 ? 'A'    // starts with 0
                       : first>>6 == 2 ? 'B'    // starts with 10
                       : first>>5 == 6 ? 'C'    // starts with 110
                       :                 'D';   // the rest
            printf("The class of the given IP address is: %c\n", class);
            break;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM