If I have an vector of unsigned char's, what's the best way to pass them to a function that only takes a vector of char's? What about a vector of signed chars?
In the conversion, is there any risk of losing data?
If I have an vector of unsigned char's, what's the best way to pass them to a function that only takes a vector of char's? What about a vector of signed chars?
In the conversion, is there any risk of losing data?
You have to copy the data to a vector of signed chars. There's no good way to just twiddle the type. However, there is no risk of data loss. Only the type changes -- the bit patterns remain the same.
A custom vector class could be written that allows magical conversion to and from signed or unsigned, but you can't do it with std::vector<T>
But then, seeing as it only differs by type, hoe about casting it to an appropriate vector type? If it's in the signed range, it should be fine, but I'm guessing it's sort of undefined behavior...
If you can change the function that you want to pass to, how about making it templated?
Otherwise, how about using a vector of char to begin with?
Thanks. So I can't do the following?
Code:std::vector<char> newOne = std::vector<char>( oldUnsignedCharVector.begin(), oldUnsignedCharVector.end() );
That should be okay, since each individual unsigned char element is type cast to char, not the whole std::vector<unsigned char> being type cast to std::vector<char>. In other words, you are copying the data to a vector of chars.Quote:
So I can't do the following?