I have the following function to convert an integer of arbitrary size to a buffer:
template<typename T>
std::string build_data_from(T val)
{
std::string result;
for (int i = 0; i < sizeof(val); i++)
{
result.insert(0, 1, char(val));
val = val >> 8;
}
return result;
};
However, invoking the template function with an unsigned char renders a warning in Visual C++ 2008:
std::string x(build_data_from<unsigned char>(1));
warning C4333: '>>' : right shift by too large amount, data loss
Is there any clean way (without using a pragma warning directive) to workaround it?
The following will get rid of he warning.
change
val = val >> 8;
to
val = val >> 7 >> 1;
or
val = (val >> 7 >> 1) & 0xff;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With