Solved

dotted IP Address to unsigned char

Posted on 2008-10-17
6
271 Views
Last Modified: 2013-12-14
Hi

I need a piece of code to convest dotted IP address to an unsigned char array, example:
192.168.0.1
will be same as:
unsigned char the_ip[4] = {192, 168, 0, 1};

preferably I don't want to use std , pure C++ code is preferred

Please advice.

Thanks from now!
0
Comment
Question by:CSecurity
  • 4
  • 2
6 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22740097
The socket library contains a nice function for that : inet_pton :

        http://linux.die.net/man/3/inet_pton

called like this :
struct sockaddr_in sa;
inet_pton(AF_INET, "192.168.0.1", &(sa.sin_addr));
 
// now sa.sin_addr.s_addr contains a 32 bit value representing the IP address

Open in new window

0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 22740125
You can of course parse the IP address yourself if you really want to.

Here's one way :
const char *ip_addr = "192.168.0.1";
unsigned char ip[4] = { 0 };
if (4 != sscanf(ip_addr, "%u.%u.%u.%u", &(ip[0]), &(ip[1]), &(ip[2]), &(ip[3]))) {
  // error parsing the IP address !!
}

Open in new window

0
 
LVL 17

Author Comment

by:CSecurity
ID: 22740133
Thanks... but I'm using VC++ 6 and I don't want to include that library... Any ideas?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Author Closing Comment

by:CSecurity
ID: 31507103
Thanks
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22741398
>> and I don't want to include that library

That is the socket library, and if you want to do socket programming, you'll need it anyway :)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22804108
The accepted code I posted earlier is not correct ... Do not use it ;)

Here's more info :

        http://www.experts-exchange.com/Programming/Languages/CPP/Q_23847222.html
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question