Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

help ? creating new string class

0 views
Skip to first unread message

m thomas

unread,
Mar 16, 2002, 4:58:21 AM3/16/02
to
Hi all,

Please excuse my ignorance before you read the problem. I may be trying to
do something that is completely stupid.

I want to create a new string class that can store binary data and I don't
particularly want to use the string STL. The problem is that when I create a
new object of this class using a pointer to a string containing binary data,
how do I know when the end of the binary data is reached?

I know it sounds like a stupid question.

To give an example, suppose I have a pointer to a string containing binary
data and I create a new string class object using the following :-

CMy_own_string_class new_str_object(ptr_to_string_containing_binary_data);

When I construct the new object, how do I know I have got all the binary
data, without doing the obvious which would be to send in the length of the
data.

Thanks for any help.

Michael
michael...@onebox.com

[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]

Francis Glassborow

unread,
Mar 16, 2002, 4:47:48 PM3/16/02
to
In article <u94son3...@corp.supernews.com>, m thomas
<michael...@onebox.com> writes

>Please excuse my ignorance before you read the problem. I may be trying to
>do something that is completely stupid.
>
>I want to create a new string class that can store binary data and I don't
>particularly want to use the string STL. The problem is that when I create a
>new object of this class using a pointer to a string containing binary data,
>how do I know when the end of the binary data is reached?
>
>I know it sounds like a stupid question.
>
>To give an example, suppose I have a pointer to a string containing binary
>data and I create a new string class object using the following :-
>
>CMy_own_string_class new_str_object(ptr_to_string_containing_binary_data);
>
>When I construct the new object, how do I know I have got all the binary
>data, without doing the obvious which would be to send in the length of the
>data.

You have few choices:

1) Each 'string' object must track the size of the string data (that is
the way most of the STL containers and basic_string work)
1A) Each string has its length encoded in the first bytes, perhaps using
the MSB of a byte to encode continuation of the count to the next byte,
so lengths of 0-127 would only use a single byte, 128-16383 would use
two bytes etc. Assuming 8-bit bytes)
2) There is an explicit bit pattern that acts as a delimiter (e.g. the
null terminator used in C-style strings). You cannot do that with binary
data unless you use an encoding (e.g. uuencode) that leaves some bit
patterns unused.

--
Francis Glassborow
Check out the ACCU Spring Conference 2002
4 Days, 4 tracks, 4+ languages, World class speakers
For details see: http://www.accu.org/events/public/accu0204.htm

Dylan Nicholson

unread,
Mar 18, 2002, 8:28:10 AM3/18/02
to
"m thomas" <michael...@onebox.com> wrote in message news:<u94son3...@corp.supernews.com>...

> Hi all,
>
> Please excuse my ignorance before you read the problem. I may be trying to
> do something that is completely stupid.
>
> I want to create a new string class that can store binary data and I don't
> particularly want to use the string STL. The problem is that when I create a
> new object of this class using a pointer to a string containing binary data,
> how do I know when the end of the binary data is reached?
>
But why wouldn't you want to use the standard string class? It copes
perfectly well with binary data (ie '0' has no special significance
once inside the string, although it is used in c_str() and when
constructing/assigning from a C-style char* string):

char bin_data[] = { 5, 0, 255, 32, 0, 128 };
std::string x(bin_data, sizeof bin_data);
std::string y(x);
assert(y.length() == sizeof bin_data);

Dylan

0 new messages