How to convert string to IP address and vice versa

前端 未结 9 781
傲寒
傲寒 2020-11-28 19:43

how can I convert a string ipAddress (struct in_addr) and vice versa? and how do I turn in unsigned long ipAddress? thanks

9条回答
  •  甜味超标
    2020-11-28 20:47

    I'm not sure if I understood the question properly.

    Anyway, are you looking for this:

    std::string ip ="192.168.1.54";
    std::stringstream s(ip);
    int a,b,c,d; //to store the 4 ints
    char ch; //to temporarily store the '.'
    s >> a >> ch >> b >> ch >> c >> ch >> d;
    std::cout << a << "  " << b << "  " << c << "  "<< d;
    

    Output:

    192  168  1  54
    

提交回复
热议问题