Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

dotted IP Address to unsigned char

Posted on 2008-10-17
6
Medium Priority
?
284 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 2000 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
Technology Partners: 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

715 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