473,386 Members | 1,820 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,386 software developers and data experts.

How can I insert null bytes in to std::string?

Pep
I have to interface to an older library that uses strings and there is
no alternative. I need to pass a string that is padded with null
bytes. So how can I append these null bytes to the std::string?

Yes I know it would be better to use something like a vector but I do
not have that option.

Yes I know that I will not be able to use std::string.c_str() but will
instead have to use std:;string.getData().

TIA.

Aug 8 '06 #1
13 6046
Pep wrote:
I have to interface to an older library that uses strings and there is
no alternative. I need to pass a string that is padded with null
bytes. So how can I append these null bytes to the std::string?
Probably using "append" member... Have you RTFM?
Yes I know it would be better to use something like a vector but I do
not have that option.
In your case it probably does not matter. Why do you think that 'vector'
has any advantage?
Yes I know that I will not be able to use std::string.c_str() but will
instead have to use std:;string.getData().
Just like with a 'vector', you can always use the address of the first
char...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 8 '06 #2
Pep

Victor Bazarov wrote:
Pep wrote:
I have to interface to an older library that uses strings and there is
no alternative. I need to pass a string that is padded with null
bytes. So how can I append these null bytes to the std::string?

Probably using "append" member... Have you RTFM?
Yes but it was suggested to me that there was a way that did not
involve using append, so as I could not find any way I decided to ask
if anyone knew of one of those "tricks".

Guess it will have to be append then.
Yes I know it would be better to use something like a vector but I do
not have that option.

In your case it probably does not matter. Why do you think that 'vector'
has any advantage?
I read something in a previous posting that suggested vectors would be
better though as you, I do not understand how other than perhaps the
poster meant that you should not in theory have nulls in strings.
>
Yes I know that I will not be able to use std::string.c_str() but will
instead have to use std:;string.getData().

Just like with a 'vector', you can always use the address of the first
char...
For some reason I opted for the getData being better than c_str as it
implies it is the correct method by it's name.
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks for the help.

Aug 8 '06 #3
In article <eb**********@news.datemas.de>,
"Victor Bazarov" <v.********@comAcast.netwrote:
Yes I know that I will not be able to use std::string.c_str() but will
instead have to use std:;string.getData().

Just like with a 'vector', you can always use the address of the first
char...
Really? I didn't think the standard guaranteed that a std::string was
contiguous like vector is?
Aug 8 '06 #4
In article <11**********************@i3g2000cwc.googlegroups. com>,
"Pep" <pe**********@yahoo.co.ukwrote:
I have to interface to an older library that uses strings and there is
no alternative. I need to pass a string that is padded with null
bytes. So how can I append these null bytes to the std::string?
The above doesn't quite make sense. C strings can't have null bytes in
the middle of them so why would you need to append null bites? If you
are talking about the null at the end of the string, then the
member-function c_str() does that for you, you don't need to.
Yes I know it would be better to use something like a vector but I do
not have that option.
I'm personally not so sure that it would be better to use "something
like a vector" but you know your problem space better than us.
Yes I know that I will not be able to use std::string.c_str() but will
instead have to use std:;string.getData().
Why can't you use c_str()? The only difference between c_str() and
data() [not getData()] is that c_str appends a null on the end. Isn't
that what you want?
Aug 8 '06 #5
Daniel T. wrote:
In article <eb**********@news.datemas.de>,
"Victor Bazarov" <v.********@comAcast.netwrote:
>>Yes I know that I will not be able to use std::string.c_str() but
will instead have to use std:;string.getData().

Just like with a 'vector', you can always use the address of the
first char...

Really? I didn't think the standard guaranteed that a std::string was
contiguous like vector is?
No, there is no such guarantee, you're correct (at least I can't find it).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 8 '06 #6
Pep wrote:
[...]
For some reason I opted for the getData
You mean 'data()', of course, no? 'data()' and 'c_str()' are essentially
the same.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 8 '06 #7

Daniel T. wrote:
In article <11**********************@i3g2000cwc.googlegroups. com>,
"Pep" <pe**********@yahoo.co.ukwrote:
I have to interface to an older library that uses strings and there is
no alternative. I need to pass a string that is padded with null
bytes. So how can I append these null bytes to the std::string?

The above doesn't quite make sense. C strings can't have null bytes in
the middle of them so why would you need to append null bites? If you
are talking about the null at the end of the string, then the
member-function c_str() does that for you, you don't need to.
This is not true - sort of anyway:

char const* weird_hello = "Weird \0hello";

does contain an embedded null and if'd call it a C-string you surely
are wrong! ;-)
I've seen a list of lines being defined with a \0 separating each
string and using an empty string as a terminator. Something like 'H'
'e' 'l' 'l' 'o' 0 'W' 'o' 'r' 'l' 'd' '!' 0 0.

My guess is the OP wanted to have two nulls at the end of the string-
Another possibility is that the interface expects the string in a
fixed-length buffer and the buffer to be containing zeroes at the end
of the string. You believe I'm kidding, but I've actually witnessed
such an interface once.
>
Yes I know it would be better to use something like a vector but I do
not have that option.

I'm personally not so sure that it would be better to use "something
like a vector" but you know your problem space better than us.
Yes I know that I will not be able to use std::string.c_str() but will
instead have to use std:;string.getData().
/Peter

Aug 8 '06 #8
* Victor Bazarov:
Daniel T. wrote:
>In article <eb**********@news.datemas.de>,
"Victor Bazarov" <v.********@comAcast.netwrote:
>>>Yes I know that I will not be able to use std::string.c_str() but
will instead have to use std:;string.getData().
Just like with a 'vector', you can always use the address of the
first char...
Really? I didn't think the standard guaranteed that a std::string was
contiguous like vector is?

No, there is no such guarantee, you're correct (at least I can't find it).
Pete Becker posted earlier (many months ago, early March?) that this
guarantee for std::basic_string was approved that day by the Library
Working Group and would be voted into the Working Draft, as I understood
it the next day or week or so, so presumably it has been so voted, and
will be present in C++0x.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 8 '06 #9
In article <11*********************@m73g2000cwd.googlegroups. com>,
"peter koch" <pe***************@gmail.comwrote:
Daniel T. wrote:
In article <11**********************@i3g2000cwc.googlegroups. com>,
"Pep" <pe**********@yahoo.co.ukwrote:
I have to interface to an older library that uses strings and there is
no alternative. I need to pass a string that is padded with null
bytes. So how can I append these null bytes to the std::string?
The above doesn't quite make sense. C strings can't have null bytes in
the middle of them so why would you need to append null bites? If you
are talking about the null at the end of the string, then the
member-function c_str() does that for you, you don't need to.

This is not true - sort of anyway:

char const* weird_hello = "Weird \0hello";

does contain an embedded null and if'd call it a C-string you surely
are wrong! ;-)
I'd call that two c-strings, but I grant that you may very well be right
and this is exactly what the OP wants.
Aug 8 '06 #10
Pep wrote:
>
Yes but it was suggested to me that there was a way that did not
involve using append, so as I could not find any way I decided to ask
if anyone knew of one of those "tricks".
Doing anything outside of the standard wouldn't
be a "trick" it would be "undefined behaviour".

--
<\___/>
/ O O \
\_____/ FTB. For email, remove my socks.

In science it often happens that scientists say, 'You know
that's a really good argument; my position is mistaken,'
and then they actually change their minds and you never
hear that old view from them again. They really do it.
It doesn't happen as often as it should, because scientists
are human and change is sometimes painful. But it happens
every day. I cannot recall the last time something like
that happened in politics or religion.

- Carl Sagan, 1987 CSICOP keynote address

Aug 8 '06 #11
In article <11**********************@h48g2000cwc.googlegroups .com>,
"Pep" <pe**********@yahoo.co.ukwrote:
Victor Bazarov wrote:
Pep wrote:
I have to interface to an older library that uses strings and there is
no alternative. I need to pass a string that is padded with null
bytes. So how can I append these null bytes to the std::string?
Probably using "append" member... Have you RTFM?

Yes but it was suggested to me that there was a way that did not
involve using append, so as I could not find any way I decided to ask
if anyone knew of one of those "tricks".

Guess it will have to be append then.
Well, you could always do: "myString[pos] = 0" if you know that pos is a
valid position.
Aug 8 '06 #12
"Pep" <pe**********@yahoo.co.ukwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
:
: Victor Bazarov wrote:
: Pep wrote:
: I have to interface to an older library that uses strings and there
is
: no alternative. I need to pass a string that is padded with null
: bytes. So how can I append these null bytes to the std::string?
: >
: Probably using "append" member... Have you RTFM?
: >
:
: Yes but it was suggested to me that there was a way that did not
: involve using append, so as I could not find any way I decided to ask
: if anyone knew of one of those "tricks".
:
: Guess it will have to be append then.

And what's wrong with it ?
s.append( 5, '\0' ); // appends five NULs, pretty straightforward?

: Yes I know it would be better to use something like a vector but I
do
: not have that option.
: >
: In your case it probably does not matter. Why do you think that
'vector'
: has any advantage?
:
: I read something in a previous posting that suggested vectors would be
: better though as you, I do not understand how other than perhaps the
: poster meant that you should not in theory have nulls in strings.

Vector historically had to be used if you wanted write
access *and* contiguous storage (because only .data()
and .c_str() were guaranteed to return a pointer to
contiguous storage, and the returned buffer is *const*).

But using &s[0] has always worked in practice,
and the standard has been revised to allow this
[ contiguity is now always guaranteed ]
: Yes I know that I will not be able to use std::string.c_str() but
will
: instead have to use std:;string.getData().
..c_str() might have an advantage over .data() in your case:
it will append an additional NULL char to the returned buffer ;)
Cheers --Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Aug 8 '06 #13
Pep

peter koch wrote:

<snip>
>
This is not true - sort of anyway:

char const* weird_hello = "Weird \0hello";

does contain an embedded null and if'd call it a C-string you surely
are wrong! ;-)
I've seen a list of lines being defined with a \0 separating each
string and using an empty string as a terminator. Something like 'H'
'e' 'l' 'l' 'o' 0 'W' 'o' 'r' 'l' 'd' '!' 0 0.

My guess is the OP wanted to have two nulls at the end of the string-
Another possibility is that the interface expects the string in a
fixed-length buffer and the buffer to be containing zeroes at the end
of the string. You believe I'm kidding, but I've actually witnessed
such an interface once.
Correct. I have taken over a large legacy system a long time ago and
still have not got to grips completely with it. The previous
programmers, all juniors, have coded a method that only caters for
strings and yet we need to send binary data through it now. You would
think that a hex encoder/decoder would deal with binary data wouldn't
you?

So without going to the lengths of changing millions of lines of code,
it would have been nice to send binary data in the string and let the
latest method in the encoder, which deals with char* arrays, to handle
it.

In the end I got completely pissed off and spent an extra 2 days
writing a proper encoder and then updating the massive code deck to
accommodate it. Now it all works correctly, encoding from binary to
string and decoding form string to binary :)

<snip>

Aug 9 '06 #14

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

Similar topics

10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
8
by: Patrick Kowalzick | last post by:
Dear NG, I would like to change the allocator of e.g. all std::strings, without changing my code. Is there a portable solution to achieve this? The only nice solution I can think of, would be...
6
by: Nemok | last post by:
Hi, I am new to STD so I have some questions about std::string because I want use it in one of my projects instead of CString. 1. Is memory set dinamicaly (like CString), can I define for...
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...
1
by: Jerry | last post by:
I'm new to c++, trying a simple test to read data form a txt file. I compiled with gcc version 3.4.4 20050721 (Red Hat 3.4.4-2). It didn't work as expected, getline() return with null string and...
2
by: FBergemann | last post by:
if i compile following sample: #include <iostream> #include <string> int main(int argc, char **argv) { std::string test = "hallo9811111z"; std::string::size_type ret;
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
5
by: Eric Lilja | last post by:
Is there something "elegant" in the standard library I can use to perform a "shifted insert" in a std::string? Let me examplify what I mean with shifted insert. Say I have: std::string foo =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.