Solved

dotted IP Address to unsigned char

Posted on 2008-10-17
6
260 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now