Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <vector>
- #include <fcntl.h>
- #include <io.h>
- #include <stdio.h>
- #include <wchar.h>
- #include <locale>
- char GautiKod(std::string str) //Hex sk pakeisti i UTF raides arba skaicius
- {
- if (str.compare("1111") == 0) return 'F';
- else if (str.compare("1110") == 0) return 'E';
- else if (str.compare("1101") == 0) return 'D';
- else if (str.compare("1100") == 0) return 'C';
- else if (str.compare("1011") == 0) return 'B';
- else if (str.compare("1010") == 0) return 'A';
- else if (str.compare("1001") == 0) return '9';
- else if (str.compare("1000") == 0) return '8';
- else if (str.compare("0111") == 0) return '7';
- else if (str.compare("0110") == 0) return '6';
- else if (str.compare("0101") == 0) return '5';
- else if (str.compare("0100") == 0) return '4';
- else if (str.compare("0011") == 0) return '3';
- else if (str.compare("0010") == 0) return '2';
- else if (str.compare("0001") == 0) return '1';
- else if (str.compare("0000") == 0) return '0';
- else if (str.compare("111") == 0) return '7';
- else if (str.compare("110") == 0) return '6';
- else if (str.compare("101") == 0) return '5';
- else if (str.compare("100") == 0) return '4';
- else if (str.compare("011") == 0) return '3';
- else if (str.compare("010") == 0) return '2';
- else if (str.compare("001") == 0) return '1';
- else if (str.compare("000") == 0) return '0';
- else if (str.compare("11") == 0) return '3';
- else if (str.compare("10") == 0) return '2';
- else if (str.compare("01") == 0) return '1';
- else if (str.compare("00") == 0) return '0';
- else if (str.compare("1") == 0) return '1';
- else if (str.compare("0") == 0) return '0';
- }
- void utf8(int decimal, int binarinisnumeris[], std::vector <std::string> baitai)
- {
- //////////////////////////////////////////////////////////////////
- int uzpilditi = 0; // kintamasis skirtas uzpildyti tuscias vietas dvejatainiu masyvu
- int masskaicius = 0;
- if (decimal <= 127) //Tikrinama kiek baitu bus reikalinga uzimti
- {
- baitai.push_back("0xxxxxxx");
- uzpilditi = 7;
- masskaicius = 1; // uzrasomas skaicius masyvu vektoriuje
- }
- else if (decimal <= 2047)
- {
- baitai.push_back("110xxxxx");
- baitai.push_back("10xxxxxx");
- uzpilditi = 11;
- masskaicius = 2;
- }
- else if (decimal <= 65535)
- {
- baitai.push_back("1110xxxx");
- baitai.push_back("10xxxxxx");
- baitai.push_back("10xxxxxx");
- uzpilditi = 16;
- masskaicius = 3;
- }
- else if (decimal <= 1114111)
- {
- baitai.push_back("11110xxx");
- baitai.push_back("10xxxxxx");
- baitai.push_back("10xxxxxx");
- baitai.push_back("10xxxxxx");
- uzpilditi = 21;
- masskaicius = 4;
- }
- //////////////////////////////////////////////////////////////////
- int i = 0, j = 0;
- while (uzpilditi > 0)
- {
- if (baitai[i][j] == 'x') //aptikus x
- {
- if (binarinisnumeris[uzpilditi - 1] == 1) //yra irasomas skaicius priklausomai nuo dvejataines masyvo skaitmenu
- baitai[i][j] = '1';
- else
- baitai[i][j] = '0';
- uzpilditi = uzpilditi - 1;
- }
- j = j + 1;
- if (j == 8) // pasiekus 8, pereinama prie kito vektoriaus masyvo
- {
- i = i + 1;
- j = 0;
- }
- }
- //////////////////////////////////////////////////////////////////
- std::string tempor;
- std::string UTFkod;
- for (int i = 0; i < masskaicius; i++)
- {
- tempor = baitai[i];
- char UTF[8];
- for (int i = 0; i < 8; i++) //uzrasomi stringo tempor[i] skaiciai i charo masyva
- UTF[i] = tempor[i];
- std::string tempor1;
- std::string tempor2;
- for (j = 0; j < 4; j = j + 1)
- tempor1.push_back(UTF[j]);
- for (j = 4; j < 8; j = j + 1)
- tempor2.push_back(UTF[j]);
- UTFkod.push_back(GautiKod(tempor1));
- UTFkod.push_back(GautiKod(tempor2));
- }
- std::cout << "UTF/8:" << UTFkod << std::endl;
- UTFkod = std::string();
- }
- void binary(int decimal, int binarinisnumeris[])
- {
- int i = 0;
- while (decimal > 0)
- {
- binarinisnumeris[i] = decimal % 2;
- decimal = decimal / 2;
- i++;
- }
- }
- void hexadecimal(int decimal, std::string hexadecimal_end)
- {
- int full_decimal, rounded_decimal, i = 0;;
- int decimal_file[16];
- char decimal_file1[16];
- std::string finalhexadecimal;
- std::ifstream hr("hexadecimal.txt");
- while (hr.peek() != EOF)
- {
- hr >> decimal_file[i] >> decimal_file1[i];
- i = i + 1;
- }
- hr.close();
- while (decimal > 0)
- {
- full_decimal = decimal / 16;
- rounded_decimal = decimal % 16;
- decimal = decimal / 16;
- for (int i = 0; i < 16; i++)
- {
- if (rounded_decimal == decimal_file[i])
- hexadecimal_end.push_back(decimal_file1[i]);
- }
- }
- // Paruosiamas rasymui
- if (hexadecimal_end.length() < 4)
- {
- if (hexadecimal_end.length() < 3)
- {
- finalhexadecimal = "U+00";
- for (i = hexadecimal_end.length() - 1; i >= 0; i--)
- finalhexadecimal.push_back(hexadecimal_end[i]);
- }
- else
- {
- finalhexadecimal = "U+0";
- for (i = hexadecimal_end.length() - 1; i >= 0; i--)
- finalhexadecimal.push_back(hexadecimal_end[i]);
- }
- }
- else
- {
- finalhexadecimal = "U+";
- for (i = hexadecimal_end.length() - 1; i >= 0; i--)
- finalhexadecimal.push_back(hexadecimal_end[i]);
- }
- //
- std::cout << finalhexadecimal << std::endl;
- }
- int main()
- {
- int decimal, choice;
- int binarinisnumeris[32] = { 0 };
- std::string hexadecimal_end;
- std::vector <std::string> baitai;
- std::cout << "Iveskite desimtaini skaiciu" << std::endl;
- std::cin >> decimal;
- hexadecimal(decimal, hexadecimal_end);
- binary(decimal, binarinisnumeris);
- utf8(decimal, binarinisnumeris, baitai);
- _setmode(_fileno(stdout), _O_U16TEXT);
- wprintf(L"char : %c", decimal);
- fflush;
- _setmode(_fileno(stdout), _O_TEXT);
- std::cout << "\n\n";
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement