Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.08 KB | None | 0 0
  1. //#include "pch.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <windows.h>
  6. #include <cstdint>
  7. #include <stdio.h>
  8. #include <conio.h>
  9. #include <sstream>
  10. #include <stdlib.h>
  11.  
  12.  
  13. using namespace std;
  14.  
  15. void converter();
  16.  
  17. void setCursorPosition( int x, int y );
  18.  
  19. bool endMessage();
  20.  
  21. string decToHex( int a );           // unused
  22.  
  23. string decToBin( int a );           // unused
  24.  
  25. int rangeCount( string a, int b );  // unused
  26.  
  27. void    decimalToHex(int dec, int hex[]);
  28.  
  29. void    decimalToBin(int dec, int bin[]);
  30.  
  31. string toUTF8(uint32_t cp);
  32.  
  33. int main()
  34. {
  35.     SetConsoleOutputCP(CP_UTF8);
  36.  
  37.     while (1)
  38.     {
  39.         converter();
  40.         if(!endMessage()) break;
  41.  
  42.         system("CLS");
  43.     }
  44.  
  45.     return 0;
  46. }
  47.  
  48. bool    endMessage()
  49. {
  50.     char end;
  51.  
  52.     setCursorPosition(38, 14);
  53.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
  54.     cout << "Jeigu norite testi darba iveskite 'Y' (arba 'y').";
  55.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  56.  
  57.     setCursorPosition(58, 15);
  58.     cin >> end;
  59.  
  60.     if (end == 'Y' || end == 'y') return true;
  61.     else
  62.     {
  63.         system("CLS");
  64.         setCursorPosition(40, 10);
  65.         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
  66.         cout << "Viso gero..";
  67.         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  68.         Sleep(2000);
  69.         return false;
  70.     }
  71. }
  72.  
  73. void    setCursorPosition( int x, int y )
  74. {
  75.     static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  76.     cout.flush();
  77.     COORD coord = { (SHORT)x, (SHORT)y };
  78.     SetConsoleCursorPosition(hOut, coord);
  79. }
  80.  
  81. string  decToHex( int a )
  82. {
  83.     int decimal = a;
  84.     stringstream my_ss;
  85.     my_ss << hex << decimal;
  86.     string res = my_ss.str();
  87.     return res;
  88. }    // unused
  89.  
  90. string  decToBin( int number )
  91. {
  92.     if (number == 0) return "0";
  93.     if (number == 1) return "1";
  94.  
  95.     if (number % 2 == 0)
  96.         return decToBin(number / 2) + "0";
  97.     else
  98.         return decToBin(number / 2) + "1";
  99. }// unused
  100.  
  101. int     rangeCount( string a, int b )
  102. {
  103.     if (a[b] == '0') return 0;
  104.     if (a[b] == '1') return 1;
  105.     if (a[b] == '2') return 2;
  106.     if (a[b] == '3') return 3;
  107.     if (a[b] == '4') return 4;
  108.     if (a[b] == '5') return 5;
  109.     if (a[b] == '6') return 6;
  110.     if (a[b] == '7') return 7;
  111.     if (a[b] == '8') return 8;
  112.     if (a[b] == '9') return 9;
  113.     if (a[b] == 'a') return 10;
  114.     if (a[b] == 'b') return 11;
  115.     if (a[b] == 'c') return 12;
  116.     if (a[b] == 'd') return 13;
  117.     if (a[b] == 'e') return 14;
  118.     if (a[b] == 'f') return 15;
  119. }
  120.  
  121. void    decimalToHex( int dec, int hex[] )
  122. {
  123.     for(int i=0; i< dec; i++)
  124.     {
  125.         hex[3]++;
  126.  
  127.         if (hex[3] == 16)
  128.         {
  129.             hex[2]++;
  130.             hex[3] = 0;
  131.         }
  132.  
  133.         if (hex[2] == 16)
  134.         {
  135.             hex[1]++;
  136.             hex[2] = 0;
  137.         }
  138.  
  139.         if (hex[1] == 16)
  140.         {
  141.             hex[0]++;
  142.             hex[1] = 0;
  143.         }
  144.     }
  145. }
  146.  
  147. void    decimalToBin( int dec, int bin[] )
  148. {
  149.     for (int i = 0; dec > 0; i++)
  150.     {
  151.         bin[i] = dec % 2;
  152.         dec /= 2;
  153.     }
  154. }
  155.  
  156. string toUTF8(uint32_t cp)
  157. {
  158.     char utf8[4];
  159.     int len = 0;
  160.  
  161.     if (cp <= 0x007F)
  162.     {
  163.         utf8[0] = static_cast<char>(cp);
  164.         len = 1;
  165.     }
  166.     else
  167.     {
  168.         if (cp <= 0x07FF)
  169.         {
  170.             utf8[0] = 0xC0;
  171.             len = 2;
  172.         }
  173.         else if (cp <= 0xFFFF)
  174.         {
  175.             utf8[0] = 0xE0;
  176.             len = 3;
  177.         }
  178.         else if (cp <= 0x10FFFF)
  179.         {
  180.             utf8[0] = 0xF0;
  181.             len = 4;
  182.         }
  183.         else
  184.             throw std::invalid_argument("invalid codepoint");
  185.  
  186.         for (int i = 1; i < len; ++i)
  187.         {
  188.             utf8[len - i] = static_cast<char>(0x80 | (cp & 0x3F));
  189.             cp >>= 6;
  190.         }
  191.  
  192.         utf8[0] |= static_cast<char>(cp);
  193.     }
  194.  
  195.     return std::string(utf8, len);
  196. }
  197.  
  198. void    converter()
  199. {
  200.     int decimal_original;
  201.     int hex[4]      = { 0 };
  202.     int binary[32]  = { 0 };
  203.     int utf[8]      = { 0 };
  204.     int count       = 0;
  205.  
  206.     setCursorPosition(47, 1);
  207.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
  208.     cout << "Iveskite desimtaini skaiciu: ";
  209.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  210.     setCursorPosition(58, 2);
  211.     cin >> decimal_original;
  212.  
  213.     string hex_str;
  214.     hex_str = decToHex(decimal_original);
  215.  
  216.     /*string bin;
  217.     bin = decToBin(decimal_original);*/
  218.  
  219.     int final_simbol = decimal_original;
  220.  
  221.     decimalToHex(decimal_original, hex);
  222.  
  223.     decimalToBin(decimal_original, binary);
  224.  
  225.     cout << endl;
  226.  
  227.     int range;
  228.     range = hex[0] * pow(16, 4) + hex[1] * pow(16, 3) + hex[2] * pow(16, 2) + hex[3];
  229.  
  230.     int bit_number;
  231.  
  232.  
  233.     if (range < 2048)
  234.     {
  235.         utf[0] =                    binary[6] * 100 + binary[5] * 10 + binary[4];
  236.         utf[1] = binary[3] * 1000 + binary[2] * 100 + binary[1] * 10 + binary[0];
  237.         bit_number = 2;
  238.     }
  239.     else if (range < 32768)
  240.     {
  241.         utf[0] = 1100 +                                                binary[10]; // 110
  242.         utf[1] = binary[9] * 1000 + binary[8] * 100 + binary[7] * 10 + binary[6];
  243.         utf[2] = 1000 +                               binary[5] * 10 + binary[4];  // 10
  244.         utf[3] = binary[3] * 1000 + binary[2] * 100 + binary[1] * 10 + binary[0];
  245.         bit_number = 4;
  246.     }
  247.     else if (range < 65537)
  248.     {
  249.         utf[0] = 1110; // 1110
  250.         utf[1] = binary[15] * 1000 + binary[14] * 100 + binary[13] * 10 + binary[12];
  251.         utf[2] = 1000 +                                 binary[11] * 10 + binary[10]; // 10
  252.         utf[3] = binary[9] * 1000 +  binary[8] * 100 +  binary[7]  * 10 + binary[6];
  253.         utf[4] = 1000 +                                 binary[5]  * 10 + binary[4];
  254.         utf[5] = binary[3] * 1000 +  binary[2] * 100 +  binary[1]  * 1  + binary[0];
  255.         bit_number = 6;
  256.     }
  257.     else if (range >= 65537)
  258.     {
  259.         utf[0] = 1111; // 1111
  260.         utf[1] =                     binary[20] * 100 + binary[19] * 10 + binary[18];
  261.         utf[2] = 1000 +                                 binary[17] * 10 + binary[16]; // 10
  262.         utf[3] = binary[15] * 1000 + binary[14] * 100 + binary[13] * 10 + binary[12];
  263.         utf[4] = 1000 + binary[11] * 10 + binary[10];
  264.         utf[5] = binary[9] * 1000 + binary[8] * 100 +   binary[7] * 10  + binary[6];
  265.         utf[6] = 1000 +                                 binary[5] * 10  + binary[4];
  266.         utf[7] = binary[3] * 1000 + binary[2] * 100 +   binary[1] * 10  + binary[0];
  267.         bit_number = 8;
  268.     }
  269.  
  270.     setCursorPosition(53, 6);
  271.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
  272.     cout << "UNICODE: ";
  273.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  274.     cout << "U + ";
  275.     for (int j = 0; j < 4; j++)
  276.     {
  277.         if (hex[j] < 10)  cout << hex[j];
  278.         if (hex[j] == 10) cout << "A";
  279.         if (hex[j] == 11) cout << "B";
  280.         if (hex[j] == 12) cout << "C";
  281.         if (hex[j] == 13) cout << "D";
  282.         if (hex[j] == 14) cout << "E";
  283.         if (hex[j] == 15) cout << "F";
  284.     }
  285.  
  286.     setCursorPosition(53, 8);
  287.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
  288.     cout << "UTF8: ";
  289.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  290.  
  291.  
  292.     int space = 0;
  293.  
  294.     for (int i = 0; i < bit_number; i++)
  295.     {
  296.         if (i%2 == 0) cout << " ";
  297.  
  298.         if (utf[i] == 0)    cout << 0;
  299.         if (utf[i] == 1)    cout << 1;
  300.         if (utf[i] == 10)   cout << 2;
  301.         if (utf[i] == 11)   cout << 3;
  302.         if (utf[i] == 100)  cout << 4;
  303.         if (utf[i] == 101)  cout << 5;
  304.         if (utf[i] == 110)  cout << 6;
  305.         if (utf[i] == 111)  cout << 7;
  306.         if (utf[i] == 1000) cout << 8;
  307.         if (utf[i] == 1001) cout << 9;
  308.         if (utf[i] == 1010) cout << "A";
  309.         if (utf[i] == 1011) cout << "B";
  310.         if (utf[i] == 1100) cout << "C";
  311.         if (utf[i] == 1101) cout << "D";
  312.         if (utf[i] == 1110) cout << "E";
  313.         if (utf[i] == 1111) cout << "F";
  314.     }
  315.  
  316.     setCursorPosition(53, 10);
  317.     string utf8 = toUTF8(final_simbol);
  318.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
  319.     cout << "SYMBOL: ";
  320.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  321.     cout << utf8 << endl;
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement