468,241 Members | 1,678 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,241 developers. It's quick & easy.

How to use htonl in IPv6

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);
}

Feb 9 '06 #1
2 7850
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 9 '06 #2

linuxer wrote:
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

Feb 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Agent | last post: by
7 posts views Thread by Torsten Schmidt | last post: by
8 posts views Thread by John Burton | last post: by
2 posts views Thread by PaulH | last post: by
8 posts views Thread by prabhuram.k | last post: by
3 posts views Thread by jiri.juranek | last post: by
3 posts views Thread by Pramod TK | last post: by
8 posts views Thread by Giampaolo Rodola' | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.