Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include "pch.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <windows.h>
- #include <cstdint>
- #include <stdio.h>
- #include <conio.h>
- #include <sstream>
- #include <stdlib.h>
- using namespace std;
- void converter();
- void setCursorPosition( int x, int y );
- bool endMessage();
- string decToHex( int a ); // unused
- string decToBin( int a ); // unused
- int rangeCount( string a, int b ); // unused
- void decimalToHex(int dec, int hex[]);
- void decimalToBin(int dec, int bin[]);
- string toUTF8(uint32_t cp);
- int main()
- {
- SetConsoleOutputCP(CP_UTF8);
- while (1)
- {
- converter();
- if(!endMessage()) break;
- system("CLS");
- }
- return 0;
- }
- bool endMessage()
- {
- char end;
- setCursorPosition(38, 14);
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
- cout << "Jeigu norite testi darba iveskite 'Y' (arba 'y').";
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
- setCursorPosition(58, 15);
- cin >> end;
- if (end == 'Y' || end == 'y') return true;
- else
- {
- system("CLS");
- setCursorPosition(40, 10);
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
- cout << "Viso gero..";
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
- Sleep(2000);
- return false;
- }
- }
- void setCursorPosition( int x, int y )
- {
- static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
- cout.flush();
- COORD coord = { (SHORT)x, (SHORT)y };
- SetConsoleCursorPosition(hOut, coord);
- }
- string decToHex( int a )
- {
- int decimal = a;
- stringstream my_ss;
- my_ss << hex << decimal;
- string res = my_ss.str();
- return res;
- } // unused
- string decToBin( int number )
- {
- if (number == 0) return "0";
- if (number == 1) return "1";
- if (number % 2 == 0)
- return decToBin(number / 2) + "0";
- else
- return decToBin(number / 2) + "1";
- }// unused
- int rangeCount( string a, int b )
- {
- if (a[b] == '0') return 0;
- if (a[b] == '1') return 1;
- if (a[b] == '2') return 2;
- if (a[b] == '3') return 3;
- if (a[b] == '4') return 4;
- if (a[b] == '5') return 5;
- if (a[b] == '6') return 6;
- if (a[b] == '7') return 7;
- if (a[b] == '8') return 8;
- if (a[b] == '9') return 9;
- if (a[b] == 'a') return 10;
- if (a[b] == 'b') return 11;
- if (a[b] == 'c') return 12;
- if (a[b] == 'd') return 13;
- if (a[b] == 'e') return 14;
- if (a[b] == 'f') return 15;
- }
- void decimalToHex( int dec, int hex[] )
- {
- for(int i=0; i< dec; i++)
- {
- hex[3]++;
- if (hex[3] == 16)
- {
- hex[2]++;
- hex[3] = 0;
- }
- if (hex[2] == 16)
- {
- hex[1]++;
- hex[2] = 0;
- }
- if (hex[1] == 16)
- {
- hex[0]++;
- hex[1] = 0;
- }
- }
- }
- void decimalToBin( int dec, int bin[] )
- {
- for (int i = 0; dec > 0; i++)
- {
- bin[i] = dec % 2;
- dec /= 2;
- }
- }
- string toUTF8(uint32_t cp)
- {
- char utf8[4];
- int len = 0;
- if (cp <= 0x007F)
- {
- utf8[0] = static_cast<char>(cp);
- len = 1;
- }
- else
- {
- if (cp <= 0x07FF)
- {
- utf8[0] = 0xC0;
- len = 2;
- }
- else if (cp <= 0xFFFF)
- {
- utf8[0] = 0xE0;
- len = 3;
- }
- else if (cp <= 0x10FFFF)
- {
- utf8[0] = 0xF0;
- len = 4;
- }
- else
- throw std::invalid_argument("invalid codepoint");
- for (int i = 1; i < len; ++i)
- {
- utf8[len - i] = static_cast<char>(0x80 | (cp & 0x3F));
- cp >>= 6;
- }
- utf8[0] |= static_cast<char>(cp);
- }
- return std::string(utf8, len);
- }
- void converter()
- {
- int decimal_original;
- int hex[4] = { 0 };
- int binary[32] = { 0 };
- int utf[8] = { 0 };
- int count = 0;
- setCursorPosition(47, 1);
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
- cout << "Iveskite desimtaini skaiciu: ";
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
- setCursorPosition(58, 2);
- cin >> decimal_original;
- string hex_str;
- hex_str = decToHex(decimal_original);
- /*string bin;
- bin = decToBin(decimal_original);*/
- int final_simbol = decimal_original;
- decimalToHex(decimal_original, hex);
- decimalToBin(decimal_original, binary);
- cout << endl;
- int range;
- range = hex[0] * pow(16, 4) + hex[1] * pow(16, 3) + hex[2] * pow(16, 2) + hex[3];
- int bit_number;
- if (range < 2048)
- {
- utf[0] = binary[6] * 100 + binary[5] * 10 + binary[4];
- utf[1] = binary[3] * 1000 + binary[2] * 100 + binary[1] * 10 + binary[0];
- bit_number = 2;
- }
- else if (range < 32768)
- {
- utf[0] = 1100 + binary[10]; // 110
- utf[1] = binary[9] * 1000 + binary[8] * 100 + binary[7] * 10 + binary[6];
- utf[2] = 1000 + binary[5] * 10 + binary[4]; // 10
- utf[3] = binary[3] * 1000 + binary[2] * 100 + binary[1] * 10 + binary[0];
- bit_number = 4;
- }
- else if (range < 65537)
- {
- utf[0] = 1110; // 1110
- utf[1] = binary[15] * 1000 + binary[14] * 100 + binary[13] * 10 + binary[12];
- utf[2] = 1000 + binary[11] * 10 + binary[10]; // 10
- utf[3] = binary[9] * 1000 + binary[8] * 100 + binary[7] * 10 + binary[6];
- utf[4] = 1000 + binary[5] * 10 + binary[4];
- utf[5] = binary[3] * 1000 + binary[2] * 100 + binary[1] * 1 + binary[0];
- bit_number = 6;
- }
- else if (range >= 65537)
- {
- utf[0] = 1111; // 1111
- utf[1] = binary[20] * 100 + binary[19] * 10 + binary[18];
- utf[2] = 1000 + binary[17] * 10 + binary[16]; // 10
- utf[3] = binary[15] * 1000 + binary[14] * 100 + binary[13] * 10 + binary[12];
- utf[4] = 1000 + binary[11] * 10 + binary[10];
- utf[5] = binary[9] * 1000 + binary[8] * 100 + binary[7] * 10 + binary[6];
- utf[6] = 1000 + binary[5] * 10 + binary[4];
- utf[7] = binary[3] * 1000 + binary[2] * 100 + binary[1] * 10 + binary[0];
- bit_number = 8;
- }
- setCursorPosition(53, 6);
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
- cout << "UNICODE: ";
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
- cout << "U + ";
- for (int j = 0; j < 4; j++)
- {
- if (hex[j] < 10) cout << hex[j];
- if (hex[j] == 10) cout << "A";
- if (hex[j] == 11) cout << "B";
- if (hex[j] == 12) cout << "C";
- if (hex[j] == 13) cout << "D";
- if (hex[j] == 14) cout << "E";
- if (hex[j] == 15) cout << "F";
- }
- setCursorPosition(53, 8);
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
- cout << "UTF8: ";
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
- int space = 0;
- for (int i = 0; i < bit_number; i++)
- {
- if (i%2 == 0) cout << " ";
- if (utf[i] == 0) cout << 0;
- if (utf[i] == 1) cout << 1;
- if (utf[i] == 10) cout << 2;
- if (utf[i] == 11) cout << 3;
- if (utf[i] == 100) cout << 4;
- if (utf[i] == 101) cout << 5;
- if (utf[i] == 110) cout << 6;
- if (utf[i] == 111) cout << 7;
- if (utf[i] == 1000) cout << 8;
- if (utf[i] == 1001) cout << 9;
- if (utf[i] == 1010) cout << "A";
- if (utf[i] == 1011) cout << "B";
- if (utf[i] == 1100) cout << "C";
- if (utf[i] == 1101) cout << "D";
- if (utf[i] == 1110) cout << "E";
- if (utf[i] == 1111) cout << "F";
- }
- setCursorPosition(53, 10);
- string utf8 = toUTF8(final_simbol);
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
- cout << "SYMBOL: ";
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
- cout << utf8 << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement