Convert IP address to integer and back

/**
     * Convert an IPv4 to a long number.
     * 
     * @param ip
     *            IPv4
     * @return a long number represents an IPv4
     */
    public static long ip2Long(String ip) {
        long num = 0;
        String[] ipArray = ip.split("\\.");
        for (int i = 0; i < ipArray.length; i++) {
            int power = 3 - i;
            num += (Integer.parseInt(ipArray[i]) % 256 * Math.pow(256, power));
        }
        return num;
    }

    /**
     * Convert a long number to an IPv4
     * 
     * @param num
     *            a long number represents an IPv4
     * @return an IPv4
     */
    public static String long2Ip(long num) {
        return ((num >> 24) & 0xFF) + "." + ((num >> 16) & 0xFF) + "."
                + ((num >> 8) & 0xFF) + "." + (num & 0xFF);
    }

转载于:https://www.cnblogs.com/ouxingning/archive/2012/09/05/convert_ip_to_integer_and_back.html

static U4 u4_s_process_s315_line(U1* u1p_a_process_line, U1* u1p_a_process_new_hex_addr) { U4 u4_t_process_result; U1* u1p_t_process_s315_pos; U1* u1p_t_process_addr_start; size_t stp_t_len_after; U1 u1_tp_store_string[(U4)STORESIZE]; U4 u4_t_address_value; U4 u4_t_new_address; /* Initialize result as FALSE */ u4_t_process_result = (U4)FALSE; u1p_t_process_s315_pos = strstr(u1p_a_process_line, "S315"); /* Search for "S315" in the input line */ /* Process only if "S315" prefix is detected */ if (NULL != u1p_t_process_s315_pos) { u1p_t_process_addr_start = u1p_t_process_s315_pos + (U4)FOUR; /* Move pointer forward to address part */ stp_t_len_after = strlen(u1p_t_process_addr_start); /* Length after the "S315" */ /* Ensure that the following string has sufficient length for processing */ if (stp_t_len_after >= (U4)ENDOFSTRING) { /* Initialize the buffer for storing new hexadecimal address */ vd_s_initialize_array(u1_tp_store_string, STORESIZE); /* Extract the first 8 characters (address part) and copy into storage buffer */ strncpy(u1_tp_store_string, u1p_t_process_addr_start, (U4)ENDOFSTRING); u1_tp_store_string[(U4)ENDOFSTRING] = '\0'; /* Null-terminate the string */ /* Convert hexadecimal address string to an integer */ u4_t_address_value = strtoul(u1_tp_store_string, NULL, (U4)SIXTEEN); u4_t_new_address = u4_t_address_value + (U4)ADD_VALUE; /* Add predefined value */ /* Format new hexadecimal address back into the output buffer */ snprintf(u1p_a_process_new_hex_addr, (U4)STORESIZE, "%08lX", u4_t_new_address); u4_t_process_result = (U4)TRUE; /* Update result as TRUE */ } } return u4_t_process_result; }在这个函数功能上添加输入查找功能,保持我设置的代码规范,名字不要变
07-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值