#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;
}
大家好,我正在尝试将IP地址从base10或base 16转换为二进制。我编写了这段代码以将IP地址转换为二进制。但是我还需要保存for循环的输出以便回答这样的问题询问并继续执行代码。 问题在链接上。禁止使用数组和数据结构。这是我要解决的任务。如果您解决了问题,我将非常高兴。 非常感谢您的帮助。Here is my code from beginnig to for loop. 我从一开始就添加了我的代码,以便您可以看到(ip4)是什么,例如程序会询问用户(输入ip地址:),然后用户输入的IP地址(168.122.1.2)就是ip地址(168等于到ip1)(122等于ip2)(1等于ip 3)(2等于ip4)。所以我有4个for循环将其转换为每个块的二进制文件。但是我需要保存二进制格式才能继续。我的问题是如何我为IP4保存了例如(00000001)。我需要另存为整数。
state = {
posts: [],
comment: ""
};
commentPost = item => {
const api = create({
baseURL: "patch-to-api-url",
headers: { Accept: "application/json" }
});
const self = this;
self.setState({ modalLoader: true });
api
.post("news/posts/" + `${item.id}` + "/comments", {
media: "",
text: this.state.comment
})
.then(response => {
console.log(response);
self.setState({ modalLoader: false });
//updating the state
this.setState(prevState => ({
posts: prevState.posts.map(el => {
if (el.id === item.id) {
return {
...el,
commentsCount: el.commentsCount + 1
};
}
return el;
})
}));
});
};
您可以测试代码。但是您应该输入10为基数,并且ip就像192.168.1.1一样,只是包含点蚂蚁整数,子网也必须是这样。
答案 0 :(得分:0)
您想以二进制形式转换IP地址,但这不是您要执行的操作。
您可以将问题简化为“此32位整数的前三位是什么?”
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");
}
}
}
}
答案 1 :(得分:0)
我需要另存为整数。
您很幸运,因为您已经将IP地址部分ip1
…ip4
保存为整数。
但是我需要保存二进制格式才能继续。
可能整数已经以简单的二进制表示形式存储,但C标准保证:
存储在…类型为unsigned char的对象中的值应为 用纯二进制符号表示。
因此,至少在将地址部分存储在unsigned char
中之后,
您可以放心地继续进行程序分配的 e。步骤,而不必担心地址的文本表示形式。要根据显示的表格确定地址类别,只需对第一个数字进行移位和掩码即可:
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;
}