views:

282

answers:

2

Hi, I'm trying to use the 'inet_addr' function to convert a char IP address, but I think since the IP Address i'm passing in to the 'inet_addr' function has leading zero's (192.169.055.075), the 'inet_addr' function is interpreting this differently. Any suggestion on how to remove the leading zeros?

Thanks

char IPAddr[20]; //192.169.055.075 ulAddr = inet_addr(IPAddr);

+4  A: 

You can use inet_pton(3) instead - it doesn't interpret leading zero as octal prefix.

Nikolai N Fetissov
A: 

How about:

string addr("192.168.055.075");
replace( addr.begin(), addr.end(), '.', ' ' );
istringstream iss(addr);
int a,b,c,d; 
iss >> a >> b >> c >> d;
ostringstream oss; 
oss << a << '.' << b << '.' << c << '.' << d;
string addrWithoutLeadingZeros( oss.str() );
markh44
Thank you....Here is also another way found to solve the problem I had.int a[4];char c[20]; if ( sscanf("192.169.055.075","%03d.%03d.%03d.%03d", a,a+1,a+2,a+3)== 4){ sprintf(c, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]); }
JB_SO