How to use htonl in IPv6

L

linuxer

Hello all,
I have the following program, which will take ipv4 address as input
in command line and convert it from host byte order to network byte
order. I need similar thing to be done for IPv6. Any help is
appriciated. Thanks in advance.

#include <stdio.h>
main( int argc, char *argv[] )
{
unsigned int ip, b1, b2, b3, b4;
ip=b1=b2=b3=b4=0;

if (sscanf(argv[1], "%d.%d.%d.%d", &b1, &b2, &b3, &b4)
== 4 && b1<=255U && b2<=255U && b3<=255U && b4<=255U) {
//For IPv4 address
ip = htonl( (b1<<24)|(b2<<16)|(b3<<8)|b4 );
}
printf("IP_TO_LONG:%ld\n", ip);
}
 
A

Alf P. Steinbach

* linuxer:
Hello all,
I have the following program, which will take ipv4 address as input
in command line and convert it from host byte order to network byte
order. I need similar thing to be done for IPv6. Any help is
appriciated. Thanks in advance.

#include <stdio.h>
main( int argc, char *argv[] )
{
unsigned int ip, b1, b2, b3, b4;
ip=b1=b2=b3=b4=0;

if (sscanf(argv[1], "%d.%d.%d.%d", &b1, &b2, &b3, &b4)
== 4 && b1<=255U && b2<=255U && b3<=255U && b4<=255U) {
//For IPv4 address
ip = htonl( (b1<<24)|(b2<<16)|(b3<<8)|b4 );
}
printf("IP_TO_LONG:%ld\n", ip);
}

"htonl" is not part of standard C++ or the standard C++ library.

So you're off-topic, and I don't know where to send you, except,
use a search engine such as Google.

The above code should not even compile.
 
M

Maxim Yegorushkin

linuxer said:
Hello all,
I have the following program, which will take ipv4 address as input
in command line and convert it from host byte order to network byte
order. I need similar thing to be done for IPv6. Any help is
appriciated. Thanks in advance.

On Linux use inet_ntop().
man inet_ntop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top