473,373 Members | 1,103 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,373 software developers and data experts.

Initialize std::string with character array

Is it possible to initialize a std::string with a character array, not
neccessarily null terminated? I.E. Given something like this:

char buffer[5];
buffer[0] = 0x01;
buffer[1] = 0x00;
buffer[2] = 'A';
buffer[3] = 'B';
buffer[4] = 'C';

The only way I know to do it now is to create a std::string and with a for
loop add each character. Is there some other, better way? I mean, I would
love to be able to say:

std::string Buffer( buffer, 5 );

Oh, you gotta be joking me! I just tried that just for the heck of it, and
it compiles and does exactly as I expect! O.o
Nov 21 '06 #1
3 20163

Jim Langston wrote:
Is it possible to initialize a std::string with a character array, not
neccessarily null terminated? I.E. Given something like this:

char buffer[5];
buffer[0] = 0x01;
buffer[1] = 0x00;
buffer[2] = 'A';
buffer[3] = 'B';
buffer[4] = 'C';

The only way I know to do it now is to create a std::string and with a for
loop add each character. Is there some other, better way? I mean, I would
love to be able to say:

std::string Buffer( buffer, 5 );

Oh, you gotta be joking me! I just tried that just for the heck of it, and
it compiles and does exactly as I expect! O.o
std::string s(buffer, buffer+5);

in your case it will be a 1 character long string with
SOH character only

Nov 21 '06 #2
dasjotre wrote:
Jim Langston wrote:
>Is it possible to initialize a std::string with a character array, not
neccessarily null terminated? I.E. Given something like this:

char buffer[5];
buffer[0] = 0x01;
buffer[1] = 0x00;
buffer[2] = 'A';
buffer[3] = 'B';
buffer[4] = 'C';

The only way I know to do it now is to create a std::string and with a for
loop add each character. Is there some other, better way? I mean, I would
love to be able to say:

std::string Buffer( buffer, 5 );

Oh, you gotta be joking me! I just tried that just for the heck of it, and
it compiles and does exactly as I expect! O.o

std::string s(buffer, buffer+5);

in your case it will be a 1 character long string with
SOH character only
Buffer(buffer, 5) constructs a string object that holds the first five
characters in buffer.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Nov 21 '06 #3
Jim Langston wrote:
Is it possible to initialize a std::string with a character array, not
neccessarily null terminated? I.E. Given something like this:

char buffer[5];
buffer[0] = 0x01;
buffer[1] = 0x00;
buffer[2] = 'A';
buffer[3] = 'B';
buffer[4] = 'C';

The only way I know to do it now is to create a std::string and with a for
loop add each character. Is there some other, better way? I mean, I would
love to be able to say:

std::string Buffer( buffer, 5 );

Oh, you gotta be joking me! I just tried that just for the heck of it, and
it compiles and does exactly as I expect! O.o
No need to be surprised. Just read the documentation.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Nov 21 '06 #4

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

Similar topics

3
by: Alexandros Frantzis | last post by:
Hello, I am trying to read a big file into a string. AFAIK std::string can read in words or "lines" from a stream. So one option is to continuously append all the "lines" from the file to the...
1
by: Christopher Benson-Manica | last post by:
I would like to know whether calling c_str() for a std::string repeatedly (with no intervening non-const function on the string) forces the repopulation of the character array or whether the string...
24
by: Julie | last post by:
I'm re-evaluating the way that I convert from a std::string to char *. (Requirement: the source is a std::string, the usable contents are char *) Here is what I've come up with: #include...
7
by: Dylan | last post by:
How do I insert a NULL character into a std::string? thanks
12
by: Flzw | last post by:
How to convert a std::string to a WCHAR* ? is there any methods or something ? I can't find. Thanks
8
by: Jason Heyes | last post by:
If s is a std::string, does &s refer to the contiguous block of characters representing s?
7
by: JustSomeGuy | last post by:
I need to make a class called uid. A UID is a unique identifier. It looks like... 1.2.3.345.1.2.4.566 This uid get transmitted over a network as 8 bit binary data. If the length of the UID is...
14
by: rohitpatel9999 | last post by:
Hi While developing any software, developer need to think about it's possible enhancement for international usage and considering UNICODE. I have read many nice articles/items in advanced C++...
14
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.