将u_int32_t的IP地址转为字符串(点分十进制)


void change_IP_str(uint32_t num, char *str){

        if(!str)
                return;

        uint32_t andval = 0xff000000;

        int i = 0;


        for( i = 3;~i;--i){

                uint32_t tmp = (num&andval) >> (i<<3);

                if(tmp){

                        int arr[] = {-1, -1, -1};
                        int cnt = 0;
                        while(tmp){
                                arr[cnt++] = tmp%10;
                                tmp /= 10;
                        }

                                while(cnt){
                                *str++ = ('0' + arr[--cnt]);
                        }
                }
                else{
                        *str++ = '0';
                }

                if(i) *str++ = '.';

                andval >>= 8;
        }



        return;
}
                     

你可能感兴趣的:(网络,c语言,linux)