1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| #include <iostream>
#include <string>
#include <cstdlib>
int main() {
std::string str="Test éé àè !";
std::cout << str << std::endl;
size_t index;
unsigned char c; // <- unsigned very important
bool is_valid;
// Display code point
for(index=0, c=str[index]; (c != '\0'); ++index) {
c=str[index];
if (c != '\0') {
std::cout << "0x" << std::hex << (int) c << ' ';
}
}
std::cout << std::endl;
// Let's go
for(index=0, c=str[index], is_valid=true; (is_valid && (c != '\0'));) {
if (c <= 0x7F) {
std::cout << c << '_';
++index;
} else if ((c >= 0xC2) && (c <= 0xDF)) {
std::cout << c << str[index + 1] << '_';
index += 2;
} else if ((c >= 0xE0) && (c <= 0xEF)) {
std::cout << c << str[index + 1] << str[index + 2] << '_';
index += 3;
} else if ((c >= 0xF0) && (c <= 0xF4)) {
std::cout << c << str[index + 1] << str[index + 2] << str[index + 3] << '_';
index += 4;
} else {
is_valid = false;
}
c = str[index];
}
std::cout << std::endl;
return EXIT_SUCCESS;
} |