473,756 Members | 6,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3743
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(tem p);
}
return strm;
}

int main() {
std::stringstre am stream("12.12.1 .12");
std::vector<int v;
while(stream.go od()) {
stream >v;
stream.ignore() ;
}
std::copy(v.beg in(),v.end(),st d::ostream_iter ator<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
16469
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope someone can help Thanks
4
5431
by: jagmeena | last post by:
Hello, I am sure this problem has been addressed before, however, I could'nt get a suitable solution to my problem. Hence I am posting here. Thanks a lot for all your help. The code I have is typedef struct Rec { unsigned char msg; unsigned long len;
8
4777
by: Ramiro Barbosa, Jr. | last post by:
All, Any ideas on how to convert the first 8 bytes of raw uninterpreted sequence of bytes from 'char array;' (populated with _binary_ data read from a socket), into a 'long id'? Thank you! -RB
15
4623
by: Bushido Hacks | last post by:
Hey c.l.c++ and/or c.g.a.opengl posters, How do I convert a hexidecimal string, traditionally used for defining colors with HTML, into a floating point array? In other words, how do I convert this: char* hex = "#FF9933"; into this:
18
26740
by: No Such Luck | last post by:
Hi all: I have an unsigned char array (size 4): unsigned char array; array = 0x00; array = 0x00; array = 0x02; array = 0xe7;
9
30521
by: Gregory.A.Book | last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have a library that reads image data and returns the data as an array of unsigned chars. The image data is stored as 4-byte floats. How can I convert the sets of 4 bytes to floats? Thanks, Greg Book
0
2033
by: anide | last post by:
Hi all I’ve some problem, I’m trying to converting a sorting algorithm from C++ to C#. In C++ I’ve compiled it using MSVC and its working properly, and in C# I’m using .NET Framework 2.0 (Visual Studio 2005). The problem occurred when I trying to opening and reading file: ============= C++ ============= void LoadSourceFile(char * fileName, unsigned char * data, int length)
0
1632
by: sam.barker0 | last post by:
Hi, How can I convert a unsigned char array1 which holds the ipaddress, into a string I am using a char array to insert the '.' between the bytes. I am using a char array because then I can convert it into a string string address(array); but when i store it in this way when I store 192 in the char array it stores "-65"
2
4585
by: sam.barker0 | last post by:
Hi guys, I am trying to form an IPV6 address string from the address bytes contained in a unsigned char buffer char tempstring; sprintf(tempstring, "%x:%x:%x:%x:%x:%x:%x:%x",htons(*((unsigned short *)(buf.GetStart()))),htons(*((unsigned short *)(buf.GetStart() +2))),htons(*((unsigned short *)(buf.GetStart()+4))),htons(*((unsigned short *)(buf.GetStart()+6))),htons(*((unsigned short *)(buf.GetStart() +8))),htons(*((unsigned short...
0
9431
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10014
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9844
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7226
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3780
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.