468,512 Members | 1,445 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

converting from IPADDRESS string to unsigned char array

Hi ,
I am trying to convert from an IPADDRESS string [say "12.12.1.2"]to a
unsigned char array[containing the octets witout the dots]

I tried to use c_str().Its was stupid because I tried to cast it with
<unsigned int.
Is there a way easily do this.

Cheers,
Sam
Mar 30 '08 #1
2 3496
On Sun, 30 Mar 2008 05:32:28 -0700, sam.barker0 wrote:
Hi ,
I am trying to convert from an IPADDRESS string [say "12.12.1.2"]to a
unsigned char array[containing the octets witout the dots]

I tried to use c_str().Its was stupid because I tried to cast it with
<unsigned int.
Is there a way easily do this.
#include <sstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

std::istream & operator>>(std::stringstream & strm, std::vector<int& v) {
if(strm.good()) {
int temp = ~0;
strm >temp;
v.push_back(temp);
}
return strm;
}

int main() {
std::stringstream stream("12.12.1.12");
std::vector<intv;
while(stream.good()) {
stream >v;
stream.ignore();
}
std::copy(v.begin(),v.end(),std::ostream_iterator< int>(std::cout,"\n"));
return 0;
}

--
OU
Mar 30 '08 #2
Hi,
Thanks for the reply.But
I need the result in an unsigned char array.
Mar 30 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Joseph Suprenant | last post: by
4 posts views Thread by jagmeena | last post: by
8 posts views Thread by Ramiro Barbosa, Jr. | last post: by
18 posts views Thread by No Such Luck | last post: by
9 posts views Thread by Gregory.A.Book | last post: by
reply views Thread by sam.barker0 | last post: by
2 posts views Thread by sam.barker0 | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.