I’m retrieving an IP address as an unsigned long integer via JSON. I’m trying to then convert this back to human readable form, ie xxx.xxx.xxx.xxx.
Example of what I receive in JSON:
"ip": 704210705
I’m struggling a bit as C was never my forte. I’m getting an EXC Bad Access error on the below:
unsigned long int addr = [[user objectForKey:@"ip"] unsignedLongValue];
struct in_addr *remoteInAddr = (struct in_addr *)addr;
char *sRemoteInAddr = inet_ntoa(*remoteInAddr);
I get the error on the char line (3).
Can anyone give me any advice?
note that the memory pointed to by
remoteis statically allocated in libc. Therefore, further calls toinet_ntoawill overwrite the previous result.To get the string properly into obj-c land, use
Or, putting everything together: