How to Convert String to Ip Address and Vice Versa

How to convert string to IP address and vice versa

Use inet_ntop() and inet_pton() if you need it other way around. Do not use inet_ntoa(), inet_aton() and similar as they are deprecated and don't support ipv6.

Here is a nice guide with quite a few examples.

// IPv4 demo of inet_ntop() and inet_pton()

struct sockaddr_in sa;
char str[INET_ADDRSTRLEN];

// store this IP address in sa:
inet_pton(AF_INET, "192.0.2.33", &(sa.sin_addr));

// now get it back and print it
inet_ntop(AF_INET, &(sa.sin_addr), str, INET_ADDRSTRLEN);

printf("%s\n", str); // prints "192.0.2.33"

How to convert string to IPAddress

If that's what you see in the debugger* then your variable really does contain those ", I'd say as a consequence of having serialized to string in a json converter; Json attributes and values are always surrounded by quotes. If you want an IPAddress as a string, it has an overridden ToString method...

  • if what you see in the debug tooltip infuses you with the slashes etc, click the to open the text visualiser; what you see there is the true exact string content (what you'd see if you wrote the string to a file and opened it in notepad)

Convert an IP string to a number and vice versa

converting an IP string to long integer:

import socket, struct

def ip2long(ip):
"""
Convert an IP string to long
"""
packedIP = socket.inet_aton(ip)
return struct.unpack("!L", packedIP)[0]

the other way around:

>>> socket.inet_ntoa(struct.pack('!L', 2130706433))
'127.0.0.1'

C - Get a string of an IP address using sin_addr [duplicate]

Expanded your code a bit to make it compile

#include <netinet/in.h>  
struct sockaddr_in clt;
int main() {
char ip[30];
strcpy(ip, (char*)inet_ntoa((struct in_addr)clt.sin_addr));
}

trying to compile it with warning enabled (always a good idea) gives a few warnings, and it sure does not work

$ gcc -Wall -Wextra -O3 ntoa.c -o ntoa && ./ntoa
ntoa.c: In function 'main':
ntoa.c:5:3: warning: implicit declaration of function 'strcpy' [-Wimplicit-function-declaration]
ntoa.c:5:3: warning: incompatible implicit declaration of built-in function 'strcpy' [enabled by default]
ntoa.c:5:3: warning: implicit declaration of function 'inet_ntoa' [-Wimplicit-function-declaration]
ntoa.c:5:14: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
ntoa.c:6:1: warning: control reaches end of non-void function [-Wreturn-type]
Segmentation fault
$

Including the headers with the missing function declarations at the top (always a good idea too)

#include <arpa/inet.h>
#include <string.h>

apparently fixes it:

$ gcc -Wall -Wextra -O3 ntoa.c -o ntoa && ./ntoa 
ntoa.c: In function 'main':
ntoa.c:8:1: warning: control reaches end of non-void function [-Wreturn-type]
$

Integer to IP Address - C

Here's a simple method to do it: The (ip >> 8), (ip >> 16) and (ip >> 24) moves the 2nd, 3rd and 4th bytes into the lower order byte, while the & 0xFF isolates the least significant byte at each step.

void print_ip(unsigned int ip)
{
unsigned char bytes[4];
bytes[0] = ip & 0xFF;
bytes[1] = (ip >> 8) & 0xFF;
bytes[2] = (ip >> 16) & 0xFF;
bytes[3] = (ip >> 24) & 0xFF;
printf("%d.%d.%d.%d\n", bytes[3], bytes[2], bytes[1], bytes[0]);
}

There is an implied bytes[0] = (ip >> 0) & 0xFF; at the first step.

Use snprintf() to print it to a string.

Convert hexadecimal string to IP Address

try this

InetAddress a = InetAddress.getByAddress(DatatypeConverter.parseHexBinary("0A064156"));

DatatypeConverter is from standard javax.xml.bind package

How to convert an IP address from NSString to unsigned int in Objective-C?

For IPv4 addresses you can use inet_aton(), which converts a string to an Internet
address:

#include <arpa/inet.h>

NSString *addrString = @"192.168.43.149";
struct in_addr addr;
if (inet_aton([addrString UTF8String], &addr) != 0) {
uint32_t ip = ntohl(addr.s_addr);
NSLog(@"%08x", ip);
} else {
NSLog(@"invalid address");
}

// Output: c0a82b95


Related Topics



Leave a reply



Submit