473,785 Members | 2,327 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 #1
13 6128
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_s tr() but will
instead have to use std:;string.get Data().
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_s tr() but will
instead have to use std:;string.get Data().

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**********@n ews.datemas.de> ,
"Victor Bazarov" <v.********@com Acast.netwrote:
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().

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************ **********@i3g2 000cwc.googlegr oups.com>,
"Pep" <pe**********@y ahoo.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_s tr() but will
instead have to use std:;string.get Data().
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**********@n ews.datemas.de> ,
"Victor Bazarov" <v.********@com Acast.netwrote:
>>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().

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************ **********@i3g2 000cwc.googlegr oups.com>,
"Pep" <pe**********@y ahoo.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_s tr() but will
instead have to use std:;string.get Data().
/Peter

Aug 8 '06 #8
* Victor Bazarov:
Daniel T. wrote:
>In article <eb**********@n ews.datemas.de> ,
"Victor Bazarov" <v.********@com Acast.netwrote:
>>>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().
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_stri ng 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************ *********@m73g2 000cwd.googlegr oups.com>,
"peter koch" <pe************ ***@gmail.comwr ote:
Daniel T. wrote:
In article <11************ **********@i3g2 000cwc.googlegr oups.com>,
"Pep" <pe**********@y ahoo.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

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

Similar topics

10
8184
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
13334
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
9201
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
15905
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
9647
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
9491
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
10357
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
10163
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
9959
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...
0
8988
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
7510
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
6744
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();...
2
3668
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.