inet_addr function and leading zeros
-
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);
-
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);
JBAK_CP wrote:
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.
Indeed, according to documentation, numbers with a leading zero are parsed as octal. Provided your format is fixed (as it looks) you may use
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]);
}eventually
c[]
contains the address without leading zeros. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
JBAK_CP wrote:
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.
Indeed, according to documentation, numbers with a leading zero are parsed as octal. Provided your format is fixed (as it looks) you may use
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]);
}eventually
c[]
contains the address without leading zeros. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]