Solved

dotted IP Address to unsigned char

Posted on 2008-10-17
6
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

759 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