473,799 Members | 3,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_s tr() but will
instead have to use std:;string.get Data().

TIA.

Aug 8 '06
13 6129
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************ **********@h48g 2000cwc.googleg roups.com>,
"Pep" <pe**********@y ahoo.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**********@y ahoo.co.ukwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.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_s tr() but
will
: instead have to use std:;string.get Data().
..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
8185
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 effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That said, is there any real reason why I can't derive an otherwise empty
22
13336
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; char c; std::string str;
8
9204
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 a namespace and another typedef to basic_string: namespace my_string {
6
11510
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 example string str1; as a class member and then add text to it. or do I have to specify it's length when defining? 2. How to convert from std::string to LPCSTR
7
11617
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 odd, an extra padding null \0 is added to the end. This is what I've written but I'm not sure if I've garanteed to have the c_str() method return a buffer that is null padded.
1
5266
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 failed to read the left data. Is there anything missing? Thanks #include <iostream>
2
5514
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
15906
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
1935
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 = "abc"; std::string shifted_foo = shifted_insert(foo, 'd'); shifted_foo should after the shifted_insert() equal "dab".
0
9685
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
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10247
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
10214
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
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7561
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
6803
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
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3751
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.