Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <string>
- using namespace std;
- ifstream in("input.txt");
- ofstream out("output.txt");
- int encode_bytes_num(int char_code) {
- if (char_code >= (1 << 16)) {
- return 4;
- } else if (char_code >= (1 << 11)) {
- return 3;
- } else if (char_code >= (1 << 7)) {
- return 2;
- } else {
- return 1;
- }
- }
- /*
- 1000 0000 - 80
- 1100 0000 - C0
- 1110 0000 - E0
- 1111 0000 - F0
- 7 - 0xxxxxxx
- 11 - 110xxxxx 10xxxxxx
- 16 - 1110xxxx 10xxxxxx 10xxxxxx
- 21 - 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
- */
- // encodes char with binary 'char_code' to utf-8 character
- string encode(int char_code) {
- int byte_cnt = encode_bytes_num(char_code);
- int first_byte[] = {0, 0, 0xC0, 0xE0, 0xF0};
- int code[4];
- code[0] = first_byte[byte_cnt];
- for (int byte = byte_cnt - 1; byte >= 1; --byte) {
- code[byte] = 0x80 + char_code % (1 << 6);
- char_code >>= 6;
- }
- code[0] += char_code;
- return string(code, code + byte_cnt);
- }
- int main() {
- int char_code;
- in >> char_code;
- while (in >> char_code) {
- out << encode(char_code);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement