Reputation: 5529
Platform is Windows with MSVC. I'm writing an encryption unpadding function.
I have many vector<int>
which are the results of a decryption function. A vector<int>
might contain a UTF-8 string, or it might be garbage if the decryption parameters were wrong. I need to subtract a number from each position, and then see if it's a valid UTF-8 string. That's the padding scheme: add a character's position to itself before encrypting.
To display the results, I assume I need to convert the vector<int>
to a vector<char>
. I can then use that as a const char[]
and print it to the console.
How do I handle the possibility of overflow and underflow when casting to a char
? After all, a char
is signed and has a range of -128 to 127 on my platform.
std::vector<char> unpad(const std::vector<int>& input) {
std::vector<char> output;
for (int i{ 0 }; i < input.size(); ++i) {
if (input[i] < -128 || input[i] > 127) {
printf("oops overflow\n");
}
output.push_back(static_cast<char>(input[i] - i)); // Padding scheme
}
output.push_back(static_cast<char>(0)); // Null termination
return output;
}
Upvotes: 0
Views: 746
Reputation: 12254
I think you're looking at this problem wrong. You don't want to cast from int to char since you'll lose information. You want to preserve the information in the int. What you need to realise is that an int is 32 bits and char 8 bits. Therefore you need 4 chars to hold all the information from a single int. To extract the information from a single int you need to use bit operators
char a = some_int & 0x000000ff;
char b = some_int & 0x0000ff00;
char c = some_int & 0x00ff0000;
char d = some_int & 0xff000000;
Now you'll have the 4 bytes (the chars) that map to the parts in a single int. You can work from there to decode utf-8.
Note that on 64 and 32 bit systems ints take 4 bytes.
Upvotes: 2