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! ]
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
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