convert a string xxx.xxx.xxx.xxx in a number (CIDR format)

Carsten A. Arnholm arnholm at offline.no
Fri May 4 14:03:49 PDT 2007


Friday, May 4, 2007, 2:07:18 PM, you wrote:
> Hi people, sorry in advance for the off-topic question.
> I have to convert a string in the format 255.255.255.0 in a number
> (in this example 30).
> code in C standard....
> to convert a single octet (255 = 7, int to int) is not a problem, the
> problem is convert a string.
> For example if I have char vett[3] where vett[0]=2 vett[1]=5 and
> vett[2]=5, how can I convert it in a int with the value 255???
> Thank you
> Franco

Hi Franco,

In C++ I would maybe do

#include <sstream>
#include <iostream>
using namespace std;
int main()
{
    char vett[] = "255";
    int ivalue = 0;
    istringstream in(vett);
    in >> ivalue;
    cout << ivalue << endl;
    return 0;
}

In plain C you could possibly use the atoi function

  int i;
  i = atoi(vett );

-- 
Carsten A. Arnholm
http://arnholm.org/
N59.776 E10.457








More information about the wx-users mailing list