Solved

dotted IP Address to unsigned char

Posted on 2008-10-17
6
266 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

773 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