A byte is not a standard type in C / C ++, so it is represented by char .
The advantage of this is that you can treat basic_string as a byte array that allows safe storage and transfer of functions. This will help to avoid memory leaks and segmentation failures that may occur when using various forms of char[] and char* .
For example, this creates a string as a byte array with zero values:
typedef basic_string<unsigned char> u_string; u_string bytes = u_string(16,'\0');
This allows you to perform standard bitwise operations with other char values, including those stored in other string variables. For example, for XOR char values ββof another u_string bytes :
u_string otherBytes = "some more chars, which are just bytes"; for(int i = 0; i < otherBytes.length(); i++) bytes[i%16] ^= (int)otherBytes[i];
Chris redford
source share