255.255.255.255.0转/24 C语言

该博客主要探讨了IPv4子网掩码的处理,包括`dealMask`函数用于将十进制数转化为二进制表示,并进行位运算。此外,`transer`函数接收一个IPv4子网掩码字符串,将其转换为二进制并输出,同时计算并显示一共有多少个1。示例中使用了子网掩码`255.255.0.0`进行演示。

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include <unistd.h>

#define MASK "255.255.0.0"
int dealMask(int x,int *buf,int ret){
        int i = ret * 8;
        while( (x/2) !=0 )
        {
                buf[i-1] = x%2;
                x = x/2;
                i--;
        }
        buf[i-1] = x%2;

        return i;

}
int transer(char *mark){
        char temp[8] = {0};
        int buf[32] = {0};
        char *str  = temp;
        char *head = mark;
        int count = 0;
        int ret = 1;
        int i = 0;
        int j = 0;
        while(*head != '\0'){
                *str = *head;
                if(*(head+1) == '.'|| *(head+1)== '\0'){
                        head = head +2;
                        dealMask(atoi(temp),buf,ret);
                        ret++;
                        memset(temp,0,sizeof(temp));
                        str = temp;
                        continue;
                }
                str++;
                head++;

        }
        for(i = 1;i <33;i++){
                if(buf[i-1] == 1){
                        j++;
                }
                printf("%d",buf[i-1]);
                if((i%8) == 0){
                        printf("\n");
                }
        }
        printf("%d",j);

}
int main(){
        transer(MASK);
        return 0;
}

uilibTrafficTest.c:130:5: error: conflicting types for 'trafficTestPacketSet' int trafficTestPacketSet(char *portListStr, char *rawPacket, UINT8 packetIndex, ^ In file included from uilibTrafficTest.c:30:0: ../../../../../../src/application/include/uilib/uilibTrafficTest.h:45:5: note: previous declaration of 'trafficTestPacketSet' was here int trafficTestPacketSet(char *portListStr, char *rawPacket, UINT8 packetIndex, ^ In file included from ../../../../../../src/platform/include/userPort/up_spec.h:11:0, from ../../../../../../src/platform/include/userPort/user_port_pvt.h:89, from ../../../../../../src/platform/include/userPort/user_port.h:88, from uilibTrafficTest.c:15: uilibTrafficTest.c: In function 'trafficTestPacketSet': uilibTrafficTest.c:154:118: error: pointer targets in passing argument 4 of 'tpConfig_objAddFieldStrForSet' differ in signedness [-Werror=pointer-sign] PFM_IF_FAIL_DONE_RET(ret, tpConfig_objAddFieldStrForSet(&stInput, TPCONFIG_INIT_ENTRY, CFG_TRAFFIC_TEST_F_SRC_MAC, srcMac), ERR_BAD_PARAM); ^ ../../../../../../src/platform/include/fepPfm.h:151:14: note: in definition of macro 'PFM_IF_FAIL_DONE_RET' int _rv = (expression);\ ^ In file included from uilibTrafficTest.c:14:0: ../../../../../../src/platform/include/midware/tpConfig.h:1084:5: note: expected 'const char *' but argument is of type 'INT8 * {aka signed char *}' int tpConfig_objAddFieldStrForSet(TPCONFIG_SET_INPUT *pInput, int type, const char *pField, const char *pStr); ^ In file included from ../../../../../../src/platform/include/userPort/up_spec.h:11:0, from ../../../../../../src/platform/include/userPort/user_port_pvt.h:89, from ../../../../../../src/platform/include/userPort/user_port.h:88, from uilibTrafficTest.c:15: uilibTrafficTest.c:155:118: error: pointer targets in passing argument 4 of 'tpConfig_objAddFieldStrForSet' differ in signedness [-Werror=pointer-sign] PFM_IF_FAIL_DONE_RET(ret, tpConfig_objAddFieldStrForSet(&stInput, TPCONFIG_INIT_ENTRY, CFG_TRAFFIC_TEST_F_DST_MAC, dstMac), ERR_BAD_PARAM); ^ ../../../../../../src/platform/include/fepPfm.h:151:14: note: in definition of macro 'PFM_IF_FAIL_DONE_RET' int _rv = (expression);\ ^ In file included from uilibTrafficTest.c:14:0: ../../../../../../src/platform/include/midware/tpConfig.h:1084:5: note: expected 'const char *' but argument is of type 'INT8 * {aka signed char *}' int tpConfig_objAddFieldStrForSet(TPCONFIG_SET_INPUT *pInput, int type, const char *pField, const char *pStr); ^ In file included from ../../../../../../src/platform/include/userPort/up_spec.h:11:0, from ../../../../../../src/platform/include/userPort/user_port_pvt.h:89, from ../../../../../../src/platform/include/userPort/user_port.h:88, from uilibTrafficTest.c:15: uilibTrafficTest.c:156:117: error: passing argument 4 of 'tpConfig_objAddFieldStrForSet' from incompatible pointer type [-Werror=incompatible-pointer-types] PFM_IF_FAIL_DONE_RET(ret, tpConfig_objAddFieldStrForSet(&stInput, TPCONFIG_INIT_ENTRY, CFG_TRAFFIC_TEST_F_SRC_IP, srcIp), ERR_BAD_PARAM); ^ ../../../../../../src/platform/include/fepPfm.h:151:14: note: in definition of macro 'PFM_IF_FAIL_DONE_RET' int _rv = (expression);\ ^ In file included from uilibTrafficTest.c:14:0: ../../../../../../src/platform/include/midware/tpConfig.h:1084:5: note: expected 'const char *' but argument is of type 'INT16 * {aka short int *}' int tpConfig_objAddFieldStrForSet(TPCONFIG_SET_INPUT *pInput, int type, const char *pField, const char *pStr); ^ In file included from ../../../../../../src/platform/include/userPort/up_spec.h:11:0, from ../../../../../../src/platform/include/userPort/user_port_pvt.h:89, from ../../../../../../src/platform/include/userPort/user_port.h:88, from uilibTrafficTest.c:15: uilibTrafficTest.c:157:117: error: passing argument 4 of 'tpConfig_objAddFieldStrForSet' from incompatible pointer type [-Werror=incompatible-pointer-types] PFM_IF_FAIL_DONE_RET(ret, tpConfig_objAddFieldStrForSet(&stInput, TPCONFIG_INIT_ENTRY, CFG_TRAFFIC_TEST_F_DST_IP, dstIp), ERR_BAD_PARAM); ^ ../../../../../../src/platform/include/fepPfm.h:151:14: note: in definition of macro 'PFM_IF_FAIL_DONE_RET' int _rv = (expression);\ ^ In file included from uilibTrafficTest.c:14:0: ../../../../../../src/platform/include/midware/tpConfig.h:1084:5: note: expected 'const char *' but argument is of type 'INT16 * {aka short int *}' int tpConfig_objAddFieldStrForSet(TPCONFIG_SET_INPUT *pInput, int type, const char *pField, const char *pStr); ^ cc1: all warnings being treated as errors CMakeFiles/uilib.dir/build.make:1313: recipe for target 'CMakeFiles/uilib.dir/uilibTrafficTest.c.o' failed make[4]: *** [CMakeFiles/uilib.dir/uilibTrafficTest.c.o] Error 1 CMakeFiles/Makefile2:107: recipe for target 'CMakeFiles/uilib.dir/all' failed make[3]: *** [CMakeFiles/uilib.dir/all] Error 2 Makefile:132: recipe for target 'all' failed make[2]: *** [all] Error 2 package/tplink/tplink-generic.mk:289: recipe for target '/project/fep_source_2/buildroot/build/ac5x_pro/build/tplink/uilib-1.0/.stamp_built' failed make[1]: *** [/project/fep_source_2/buildroot/build/ac5x_pro/build/tplink/uilib-1.0/.stamp_built] Error 2 Makefile:84: recipe for target '_all' failed make: *** [_all] Error 2 解释下报错
最新发布
10-28
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值