如何在std :: string中插入空字节? [英] How can I insert null bytes in to std::string?

查看:76
本文介绍了如何在std :: string中插入空字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须连接到使用字符串的旧库,并且

无可奈何。我需要传递一个用null填充的字符串

字节。那么如何将这些空字节附加到std :: string?


是的我知道使用像vector这样的东西会更好

没有那个选项。


是的我知道我将无法使用std :: string.c_str()但是

而是必须使用std :; string.getData()。


TIA。

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.

推荐答案

Pep写道:
Pep wrote:

我必须连接到使用字符串的旧库,并且

无可奈何。我需要传递一个用null填充的字符串

字节。那么如何将这些空字节附加到std :: string呢?
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?



可能使用追加会员......你有RTFM吗?

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.



在你的情况下,它可能没关系。为什么你认为''vector''

有什么优势?

In your case it probably does not matter. Why do you think that ''vector''
has any advantage?


是的我知道我将无法使用std :: string.c_str()但是

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



就像使用''vector''一样,你总是可以使用第一个

char的地址...


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

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




Victor Bazarov写道:

Victor Bazarov wrote:

Pep写道:
Pep wrote:

我必须连接到使用字符串的旧库,并且

别无选择。我需要传递一个用null填充的字符串

字节。那么如何将这些空字节附加到std :: string呢?
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?



可能使用追加会员......你有RTFM吗?


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.


是的我知道使用像vector这样的东西会更好

没有那个选项。
Yes I know it would be better to use something like a vector but I do
not have that option.



在你的情况下,它可能没关系。为什么你认为''vector''

有什么优势?


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.


>
>

是的我知道我将无法使用std :: string.c_str()但是

而是必须使用std :; string.getData()。
Yes I know that I will not be able to use std::string.c_str() but will
instead have to use std:;string.getData().



就像使用''vector''一样,你总是可以使用第一个

char的地址...


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



出于某种原因,我选择了比c_str更好的getData,因为它/ b $ b意味着它的名称是正确的方法。 />

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

-

请删除大写''A'当通过电子邮件回复时

我没有回复最热门的回复,请不要问
>
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.


文章< eb ********** @ news.datemas.de>,

Victor Bazarov < v。******** @ comAcast.netwrote:
In article <eb**********@news.datemas.de>,
"Victor Bazarov" <v.********@comAcast.netwrote:

是的我知道我不会使用std :: string.c_str()但是

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



就像使用''vector''一样,你总是可以使用第一个

char的地址...


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



真的吗?我不认为标准保证std :: string是

像vector一样连续吗?

Really? I didn''t think the standard guaranteed that a std::string was
contiguous like vector is?


这篇关于如何在std :: string中插入空字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆