C++ STL's String eqivalent for Binary Data
I'd use std::vector<unsigned char>
. Most operations you need can be done using the STL with iterator ranges. Also, remember that if you really need the raw data &v[0]
is guaranteed to give a pointer to the underlying array.
You can use std::string
also for binary data. The length of the data in std::string
is stored explicitly and not determined by null-termination, so null-bytes don't have special meaning in a std::string
.
std::string
is often more convenient than std::vector<char>
because it provides many methods that are useful to work with binary data but not provided by vector
. To parse/create binary data it is useful to have things like substr()
, overloads for +
and std::stringstream
at your disposal. On vectors the algorithms from <algorithm>
can be used to achieve the same effects, but it's more clumsy than the string methods. If you just act on "sequences of characters", std::string
gives you the methods you usually want, even if these sequences happen to contain "binary" data.
You should use std::vector<unsigned char>
or std::vector<uint8_t>
(if you have a modern stdint.h
header). There's nothing wrong with using unsigned char[]
or uint8_t[]
if you are working with fixed size buffers. Where std::vector
really shines is when you need to grow or append to your buffers frequently. STL iterators have the same semantics as pointers, so STL algorithms will work equally well with std::vector
and plain old arrays.
And as CAdaker pointed out, the expression &v[0]
is guaranteed to give you the underlying pointer to the vector's buffer (and it's guaranteed to be one contiguous block of memory). This guarantee was added in an addendum to the C++ standard.
Personally, I'd avoid using std::string
to manipulate arbitrary byte buffers, since I think it's potentially confusing, but it's not an unheard of practice.