Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

c++ - How to convert standard IP address format string to hex and long?

Does anyone know how to get the IP address in decimal or hex from standard IP address format string ("xxx.xxx.xxx.xxx")?

I've tried to use the inet_addr() function but didn't get the right result.

I tested it on "84.52.184.224"

the function returned 3770168404 which is not correct (the correct result is 1412741344).

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have just got the bytes reversed from what you expected - they are in network byte order

3770168404 = 0xE0 B8 34 54     network byte order
               |         |
                       /
                      /
                     /
                    /
                   /
                  /     
                 /    
                /      
               |        |
1412741344 = 0x54 34 B8 E0     machine order

You could use ntohl() convert from network order to machine order.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...