[Botan-devel] Converting SecureVector<byte> to string

Jack Lloyd lloyd at randombit.net
Sat Mar 28 16:12:22 EDT 2009


On Sat, Mar 28, 2009 at 03:07:06PM -0400, Z. S. O. wrote:
> That seems to work well, but it's not clear to me how to go the other
> direction (string to SecureVector<byte>) since a byte is smaller than a char
> so the compiler keeps yelling at me for losing precision.

It's not really a loss of precision, just a conversion from signed to
unsigned which is a normally frowned upon conversion in the C type
system (I use the term type system loosely here). In theory char is
not always a byte but in practice in C/C++ it more or less has to be
(and botan's byte typedef is just an unsigned char on the assumption
that a byte is 8 bits; botan would not work correctly at all on any
system that did not use 8 bit chars but fortunately as I mentioned
such systems are rare to nonexistent (at least these days, I guess
this limitation would mean botan would have problems running on a
PDP-8 but I can't say this keeps me up at night much)).

All you should need is a reinterpret_cast, which is what C++ calls the
hey-compiler-just-shut-up-and-trust-me typecast operator.

SecureVector<byte> vec(reinterpret_cast<const byte*>(str.data()),
                       str.length());

-Jack



More information about the botan-devel mailing list