Thanks for the reply dude, but correct me if I am wrong. Doesn't above statement returns address as inet_addr_t? I don't know how this will return long value?
This printed the string back to me. Note: I changed your unsigned chars to unsigned shorts, else it gives me the error "stack around a was corrupted", though it seems to still work correctly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <cstdio>
int main() {
char arr[] = "192.168.1.102\0 "; //extended to be able to fit up to maximum length
unsignedshort a, b, c, d;
sscanf(arr, "%hu.%hu.%hu.%hu", &a, &b, &c, &d );
arr[0] = '\0'; // cleared to make sure it's not just the same string coming back
unsignedlong ipAddr = ( a << 24 ) | ( b << 16 ) | ( c << 8 ) | d;
a = (ipAddr & (0xff << 24)) >> 24;
b = (ipAddr & (0xff << 16)) >> 16;
c = (ipAddr & (0xff << 8)) >> 8;
d = ipAddr & 0xff;
sprintf(arr, "%hu.%hu.%hu.%hu", a, b, c, d);
puts(arr);
}