Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <vector>
  6. #include <fcntl.h>
  7. #include <io.h>
  8. #include <stdio.h>
  9. #include <wchar.h>
  10. #include <locale>
  11.  
  12.  
  13. char GautiKod(std::string str) //Hex sk pakeisti i UTF raides arba skaicius
  14. {
  15. if (str.compare("1111") == 0) return 'F';
  16. else if (str.compare("1110") == 0) return 'E';
  17. else if (str.compare("1101") == 0) return 'D';
  18. else if (str.compare("1100") == 0) return 'C';
  19. else if (str.compare("1011") == 0) return 'B';
  20. else if (str.compare("1010") == 0) return 'A';
  21. else if (str.compare("1001") == 0) return '9';
  22. else if (str.compare("1000") == 0) return '8';
  23. else if (str.compare("0111") == 0) return '7';
  24. else if (str.compare("0110") == 0) return '6';
  25. else if (str.compare("0101") == 0) return '5';
  26. else if (str.compare("0100") == 0) return '4';
  27. else if (str.compare("0011") == 0) return '3';
  28. else if (str.compare("0010") == 0) return '2';
  29. else if (str.compare("0001") == 0) return '1';
  30. else if (str.compare("0000") == 0) return '0';
  31. else if (str.compare("111") == 0) return '7';
  32. else if (str.compare("110") == 0) return '6';
  33. else if (str.compare("101") == 0) return '5';
  34. else if (str.compare("100") == 0) return '4';
  35. else if (str.compare("011") == 0) return '3';
  36. else if (str.compare("010") == 0) return '2';
  37. else if (str.compare("001") == 0) return '1';
  38. else if (str.compare("000") == 0) return '0';
  39. else if (str.compare("11") == 0) return '3';
  40. else if (str.compare("10") == 0) return '2';
  41. else if (str.compare("01") == 0) return '1';
  42. else if (str.compare("00") == 0) return '0';
  43. else if (str.compare("1") == 0) return '1';
  44. else if (str.compare("0") == 0) return '0';
  45. }
  46.  
  47. void utf8(int decimal, int binarinisnumeris[], std::vector <std::string> baitai)
  48. {
  49. //////////////////////////////////////////////////////////////////
  50. int uzpilditi = 0; // kintamasis skirtas uzpildyti tuscias vietas dvejatainiu masyvu
  51. int masskaicius = 0;
  52. if (decimal <= 127) //Tikrinama kiek baitu bus reikalinga uzimti
  53. {
  54. baitai.push_back("0xxxxxxx");
  55. uzpilditi = 7;
  56. masskaicius = 1; // uzrasomas skaicius masyvu vektoriuje
  57.  
  58. }
  59. else if (decimal <= 2047)
  60. {
  61. baitai.push_back("110xxxxx");
  62. baitai.push_back("10xxxxxx");
  63. uzpilditi = 11;
  64. masskaicius = 2;
  65. }
  66. else if (decimal <= 65535)
  67. {
  68. baitai.push_back("1110xxxx");
  69. baitai.push_back("10xxxxxx");
  70. baitai.push_back("10xxxxxx");
  71. uzpilditi = 16;
  72. masskaicius = 3;
  73. }
  74. else if (decimal <= 1114111)
  75. {
  76. baitai.push_back("11110xxx");
  77. baitai.push_back("10xxxxxx");
  78. baitai.push_back("10xxxxxx");
  79. baitai.push_back("10xxxxxx");
  80. uzpilditi = 21;
  81. masskaicius = 4;
  82. }
  83. //////////////////////////////////////////////////////////////////
  84. int i = 0, j = 0;
  85. while (uzpilditi > 0)
  86. {
  87. if (baitai[i][j] == 'x') //aptikus x
  88. {
  89. if (binarinisnumeris[uzpilditi - 1] == 1) //yra irasomas skaicius priklausomai nuo dvejataines masyvo skaitmenu
  90. baitai[i][j] = '1';
  91. else
  92. baitai[i][j] = '0';
  93.  
  94. uzpilditi = uzpilditi - 1;
  95. }
  96. j = j + 1;
  97. if (j == 8) // pasiekus 8, pereinama prie kito vektoriaus masyvo
  98. {
  99. i = i + 1;
  100. j = 0;
  101. }
  102. }
  103. //////////////////////////////////////////////////////////////////
  104.  
  105. std::string tempor;
  106. std::string UTFkod;
  107. for (int i = 0; i < masskaicius; i++)
  108. {
  109. tempor = baitai[i];
  110. char UTF[8];
  111.  
  112. for (int i = 0; i < 8; i++) //uzrasomi stringo tempor[i] skaiciai i charo masyva
  113. UTF[i] = tempor[i];
  114. std::string tempor1;
  115. std::string tempor2;
  116.  
  117. for (j = 0; j < 4; j = j + 1)
  118. tempor1.push_back(UTF[j]);
  119. for (j = 4; j < 8; j = j + 1)
  120. tempor2.push_back(UTF[j]);
  121. UTFkod.push_back(GautiKod(tempor1));
  122. UTFkod.push_back(GautiKod(tempor2));
  123. }
  124. std::cout << "UTF/8:" << UTFkod << std::endl;
  125. UTFkod = std::string();
  126.  
  127.  
  128. }
  129.  
  130. void binary(int decimal, int binarinisnumeris[])
  131. {
  132. int i = 0;
  133. while (decimal > 0)
  134. {
  135. binarinisnumeris[i] = decimal % 2;
  136. decimal = decimal / 2;
  137. i++;
  138. }
  139.  
  140. }
  141.  
  142. void hexadecimal(int decimal, std::string hexadecimal_end)
  143. {
  144. int full_decimal, rounded_decimal, i = 0;;
  145. int decimal_file[16];
  146. char decimal_file1[16];
  147. std::string finalhexadecimal;
  148. std::ifstream hr("hexadecimal.txt");
  149. while (hr.peek() != EOF)
  150. {
  151. hr >> decimal_file[i] >> decimal_file1[i];
  152. i = i + 1;
  153. }
  154. hr.close();
  155. while (decimal > 0)
  156. {
  157. full_decimal = decimal / 16;
  158. rounded_decimal = decimal % 16;
  159. decimal = decimal / 16;
  160. for (int i = 0; i < 16; i++)
  161. {
  162. if (rounded_decimal == decimal_file[i])
  163. hexadecimal_end.push_back(decimal_file1[i]);
  164.  
  165. }
  166. }
  167. // Paruosiamas rasymui
  168. if (hexadecimal_end.length() < 4)
  169. {
  170. if (hexadecimal_end.length() < 3)
  171. {
  172. finalhexadecimal = "U+00";
  173. for (i = hexadecimal_end.length() - 1; i >= 0; i--)
  174. finalhexadecimal.push_back(hexadecimal_end[i]);
  175.  
  176. }
  177. else
  178. {
  179. finalhexadecimal = "U+0";
  180. for (i = hexadecimal_end.length() - 1; i >= 0; i--)
  181. finalhexadecimal.push_back(hexadecimal_end[i]);
  182. }
  183. }
  184. else
  185. {
  186. finalhexadecimal = "U+";
  187. for (i = hexadecimal_end.length() - 1; i >= 0; i--)
  188. finalhexadecimal.push_back(hexadecimal_end[i]);
  189. }
  190. //
  191. std::cout << finalhexadecimal << std::endl;
  192.  
  193. }
  194.  
  195. int main()
  196. {
  197. int decimal, choice;
  198. int binarinisnumeris[32] = { 0 };
  199. std::string hexadecimal_end;
  200. std::vector <std::string> baitai;
  201.  
  202.  
  203.  
  204. std::cout << "Iveskite desimtaini skaiciu" << std::endl;
  205. std::cin >> decimal;
  206. hexadecimal(decimal, hexadecimal_end);
  207. binary(decimal, binarinisnumeris);
  208. utf8(decimal, binarinisnumeris, baitai);
  209. _setmode(_fileno(stdout), _O_U16TEXT);
  210. wprintf(L"char : %c", decimal);
  211. fflush;
  212. _setmode(_fileno(stdout), _O_TEXT);
  213. std::cout << "\n\n";
  214. system("pause");
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement