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

Z. S. O. tiredashell at gmail.com
Sat Mar 28 16:25:56 EDT 2009


Thank you very much, it works great!

On Sat, Mar 28, 2009 at 4:12 PM, Jack Lloyd <lloyd at randombit.net> wrote:

> 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
> _______________________________________________
> botan-devel mailing list
> botan-devel at randombit.net
> http://lists.randombit.net/mailman/listinfo/botan-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.randombit.net/pipermail/botan-devel/attachments/20090328/854fd82f/attachment.html>


More information about the botan-devel mailing list