Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include "pch.h"
- // utf8 convetreis.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <vector>
- #include <map>
- #include <locale>
- #include <wchar.h>
- #include <io.h>
- #include <fcntl.h>
- #include <windows.h>
- using namespace std;
- ofstream fr("utf8.txt");
- string decToHex(int raide) {
- string hex;
- int liekana = raide;
- int sveikaDalis = raide;
- while (sveikaDalis >= 1) { //skaicius dalinamas is 16 ir randama liekana is padalinto skaiciaus
- liekana = sveikaDalis % 16;
- sveikaDalis /= 16;
- if (liekana < 10) hex.insert(0, to_string(liekana));
- else if (liekana == 10) hex.insert(0, "A");
- else if (liekana == 11) hex.insert(0, "B");
- else if (liekana == 12) hex.insert(0, "C");
- else if (liekana == 13) hex.insert(0, "D");
- else if (liekana == 14) hex.insert(0, "E");
- else if (liekana == 15) hex.insert(0, "F");
- }
- return hex;
- }
- string hexToBin(string sk) {
- string bin;
- for (int i = 0; i < sk.length(); i++) {
- if (sk[i] == '0') bin.append("0000");
- else if (sk[i] == '1') bin.append("0001");
- else if (sk[i] == '2') bin.append("0010");
- else if (sk[i] == '3') bin.append("0011");
- else if (sk[i] == '4') bin.append("0100");
- else if (sk[i] == '5') bin.append("0101");
- else if (sk[i] == '6') bin.append("0110");
- else if (sk[i] == '7') bin.append("0111");
- else if (sk[i] == '8') bin.append("1000");
- else if (sk[i] == '9') bin.append("1001");
- else if (sk[i] == 'A') bin.append("1010");
- else if (sk[i] == 'B') bin.append("1011");
- else if (sk[i] == 'C') bin.append("1100");
- else if (sk[i] == 'D') bin.append("1101");
- else if (sk[i] == 'E') bin.append("1110");
- else if (sk[i] == 'F') bin.append("1111");
- }
- return bin;
- }
- string binToHex(string unicode) {
- string utf;
- for (int i = 0; i < unicode.length(); i += 4) { //paimami 4 skaiciai is ivesto skaiciaus ir prilyginami
- string ch;
- ch = unicode.substr(i, 4);
- if (ch == "0000") utf.append("0");
- else if (ch == "0001") utf.append("1");
- else if (ch == "0010") utf.append("2");
- else if (ch == "0011") utf.append("3");
- else if (ch == "0100") utf.append("4");
- else if (ch == "0101") utf.append("5");
- else if (ch == "0110") utf.append("6");
- else if (ch == "0111") utf.append("7");
- else if (ch == "1000") utf.append("8");
- else if (ch == "1001") utf.append("9");
- else if (ch == "1010") utf.append("A");
- else if (ch == "1011") utf.append("B");
- else if (ch == "1100") utf.append("C");
- else if (ch == "1101") utf.append("D");
- else if (ch == "1110") utf.append("E");
- else if (ch == "1111") utf.append("F");
- }
- return utf;
- }
- string convert(int level, string bin) {
- string utf;
- while (1) {
- if (bin.length() > 0) {
- utf.insert(0, 1, bin.back()); //imama paskutinis string char
- bin = bin.substr(0, bin.size() - 1); //nutrinamas paskutinis char
- }
- else utf.insert(0, "0");
- if (utf.length() == 13 && level == 2) {
- utf.insert(0, "110");
- break;
- }
- else if (utf.length() == 20 && level == 3) {
- utf.insert(0, "1110");
- break;
- }
- else if (utf.length() == 27 && level == 4) {
- utf.insert(0, "11110");
- break;
- }
- if (utf.length() == 6 || utf.length() == 14 || utf.length() == 22) utf.insert(0, "10");
- }
- return utf;
- }
- void library(map<int, int> &lib) {
- ifstream fd("librarynochar.txt");
- string blank;
- int unicode;
- int codePage;
- unsigned char ch;
- for (int i = 128; i < 256; i++) {
- fd >> blank >> codePage >> unicode;
- lib[codePage] = unicode;
- }
- }
- void hexToFullChar(string a) {
- for (int i = 0; i < a.length(); i+=2) {
- int b = 0;
- if (a[i] >= '0' && a[i] <= '9') {
- b += (a[i] - '0') * 16;
- }
- else {
- b += (a[i] - 'A' + 10) * 16;
- }
- if (a[i+1] >= '0' && a[i+1] <= '9') {
- b += (a[i+1] - '0');
- }
- else {
- b += (a[i+1] - 'A' + 10);
- }
- char ch = b;
- fr << ch;
- }
- }
- int main() {
- map<int, int> lib;
- library(lib);
- ifstream fd("386intel.txt");
- fd >> noskipws; //kad skaitytu ir- space
- while (!fd.eof()) {
- unsigned char ch;
- fd >> ch;
- int raide = ch;
- if (raide < 128) {
- fr << ch;
- }
- else {
- raide = lib[raide];
- string hex = decToHex(raide);
- string bin = hexToBin(hex);
- string utf;
- if (raide < 2048) {
- utf = binToHex(convert(2, bin));
- }
- else if (raide < 65536) {
- utf = binToHex(convert(3, bin));
- }
- else {
- utf = binToHex(convert(4, bin));
- }
- hexToFullChar(utf);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement