473,809 Members | 2,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert string into an array

Hi,
I'm trying to convert a string something like this
"{201,23,240,56 ,23,45,34,23}" into an array in C++
Please help.
Thanks,
Sudzzz

Jul 3 '07
11 5063
"James Kanze" <ja*********@gm ail.comwrote in message
news:11******** **************@ n60g2000hse.goo glegroups.com.. .
On Jul 3, 10:46 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
I would use a stringstream.

Just curious, but why a stringstream, and not an istringstream?
I don't know. I use stringstream for all types of conversion and it works.
What real advantage would istringstream give me?
[...]
std::string str_array = "{201,23,240,56 ,23,45,34,23}";
std::stringstre am Buffer;
Buffer << str_array;

Why those two lines, instead of simply:

std::istringstr eam Buffer( str_array ) ;
Becuase I normally use stringstring in one of my templates:

template<typena me T, typename F T StrmConvert( const F from )
{
std::stringstre am temp;
temp << from;
T to = T();
temp >to;
return to;
}

If I change this to

std::string temp( from );

then I get compiler warnings at times depending on F.

warning C4244: 'argument' : conversion from 'const float' to
'std::_Iosb<_Du mmy>::openmode' , possible loss of data

and I go for 0% warnings in my code. I know that the form I use doesn't
give any warnings.
char Trash;
Buffer >Trash; // Throw away (

Throw away, or verify, as a syntax check? (In my experience,
you can never verify input too much.)
Agreed, but in most instances where I'm using stringstream for conversion
I've verified the data before I try to convert it, or the output makes it
obvious there was a problem. With user input data I would definately do
syntax checking of the string before/during/after the conversion.
int Value;
std::vector<int Data;
while ( Buffer >Value )

while ( Buffer >Value && Trash != '}' )
This is really not needed though, unless there is extraneous data after
the ) such as

{201,23,240,56, 23,45,34,23}75

etc... (not going to comment on the rest)
{

And

if ( Trash != (Data.empty() ? '{' : ',') ) {
Buffer.setstate ( std::ios::failb it ) ;
}

here.
Data.push_back( Value );
Buffer >Trash; // Throw away , or )
}

And a final error check:

if ( ! (Buffer >std::ws && Buffer.get() == EOF ) {
error ...
}
// Display vector data
for ( std::vector<int >::iterator it = Data.begin(); it !=
Data.end();
++it )
std::cout << *it << " ";
}
(But I like your basic algorithm better than the one I
suggested.)

Jul 5 '07 #11
On Jul 5, 4:36 am, "Jim Langston" <tazmas...@rock etmail.comwrote :
"James Kanze" <james.ka...@gm ail.comwrote in message
news:11******** **************@ n60g2000hse.goo glegroups.com.. .
On Jul 3, 10:46 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
I would use a stringstream.
Just curious, but why a stringstream, and not an istringstream?
I don't know. I use stringstream for all types of conversion
and it works. What real advantage would istringstream give
me?
It seems clearer to me if you're only going to be reading. I
use istringstream for input conversions (from text to whatever),
and ostringstream for output conversions (to text from
whatever). Somehow, it just seems clearer to say up front what
I'm going to do.
[...]
std::string str_array = "{201,23,240,56 ,23,45,34,23}";
std::stringstre am Buffer;
Buffer << str_array;
Why those two lines, instead of simply:
std::istringstr eam Buffer( str_array ) ;
Becuase I normally use stringstring in one of my templates:
template<typena me T, typename F T StrmConvert( const F from )
{
std::stringstre am temp;
temp << from;
T to = T();
temp >to;
return to;
}
Sort of boost::lexical_ cast, in sum. That's a different
context. (I'm not totally convinced that boost::lexical_ cast is
a good idea. I'm not too hot on the idea that you can convert
anything to anything else, and never get a compiler error.)
If I change this to
std::string temp( from );
then I get compiler warnings at times depending on F.
warning C4244: 'argument' : conversion from 'const float' to
'std::_Iosb<_Du mmy>::openmode' , possible loss of data
and I go for 0% warnings in my code. I know that the form I
use doesn't give any warnings.
One could argue that since you're doing two conversions, you
should have two conversion objects, i.g.:

std::istringstr eam in ;
in << from ;
std::ostringstr eam out( in.str() ) ;
T to ;
out >to ;
return to ;

I'm not sure. As I said, I'm not too hot on this idea to begin
with. (If I were doing it, I'd certainly add a lot of error
handling. But I presume you've just stripped it out to make it
shorter for posting.)
char Trash;
Buffer >Trash; // Throw away (
Throw away, or verify, as a syntax check? (In my experience,
you can never verify input too much.)
Agreed, but in most instances where I'm using stringstream for
conversion I've verified the data before I try to convert it,
or the output makes it obvious there was a problem.
In most cases, I have too. Nothing like boost::regex
beforehand, to know what I'm dealing with. (But I thought I'd
mentionned that.) If you don't know the length in advance,
however, it's probably easier to do the checking dynamically.

I don't know the full context either. It might be worth
considering creating a decorator type for which you define an
operator>>, and use that, something like:

source >ArrayReader( array ) ;
With user input data I would definately do syntax checking of
the string before/during/after the conversion.
int Value;
std::vector<int Data;
while ( Buffer >Value )
while ( Buffer >Value && Trash != '}' )
This is really not needed though, unless there is extraneous data after
the ) such as
{201,23,240,56, 23,45,34,23}75
etc...
Yes. The real question is whether the string has been format
checked before hand, or not. If it has, then no further
checking should be necessary. If it hasn't, you probably want
to verify everything.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 5 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6116
by: David Lawson | last post by:
I know how to conver a string to an array of strings, but I need to convert an ascii string to an array of integers (really unsigned chars). Eg, $str ="ABC"; needs to convert to something like this: $buf = array(0x41, 0x42, 0x43); Anyone know how? I haven't been able to find a way.
27
51740
by: Trep | last post by:
Hi there! I've been having a lot of difficult trying to figure out a way to convert a terminated char array to a system::string for use in Visual C++ .NET 2003. This is necessary because I have some legcay C code that needs to process a string taken from a textbox, then I need to re-display the string as the textbox->Text. I easily found how to convert from system::string to char but I can't figure out how to go the other way!!
6
10237
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that the data converted from the byte array into a string , is not what i expect. example: - the data returned from the socket is (byte array) "Usuario Sin Atribuciones Para Esta Función"
6
12771
by: Allan Ebdrup | last post by:
How do I easily convert a int to a string? Kind Regards, Allan Ebdrup
6
53730
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how do I convert it to: dim myStr as string = "11,22,33,44"
12
13570
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} System.Text.UTF8Encoding str = new System.Text.UTF8Encoding(); string s = new string((char)107, 1); s += new string((char)62, 1);
19
5351
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that str(object) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given, returns the empty string, ''.
9
35860
by: myotheraccount | last post by:
Hello, Is there a way to convert a DataRow to a StringArray, without looping through all of the items of the DataRow? Basically, I'm trying to get the results of a query and put them into a listbox. Right now it is done like this:
5
4855
by: da1978 | last post by:
Hi experts, I need to convert a string or a Byte array to a string byte array. Its relatively easy to convert a string to an char array or a byte array but not a STRING byte array. i.e. Dim Array() As Char Dim strwork As String = "76A3kj9d6" Array = strwork.ToCharArray OR
0
10792
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
10643
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
10378
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...
1
10391
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
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
7664
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
6881
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
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.