Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. // ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include "pch.h"
  5. // utf8 convetreis.cpp : This file contains the 'main' function. Program execution begins and ends there.
  6. //
  7.  
  8. #include <iostream>
  9. #include <string>
  10. #include <fstream>
  11. #include <vector>
  12. #include <map>
  13. #include <locale>
  14. #include <wchar.h>
  15. #include <io.h>
  16. #include <fcntl.h>
  17. #include <windows.h>
  18.  
  19.  
  20.  
  21. using namespace std;
  22. ofstream fr("utf8.txt");
  23. string decToHex(int raide) {
  24. string hex;
  25. int liekana = raide;
  26. int sveikaDalis = raide;
  27.  
  28. while (sveikaDalis >= 1) { //skaicius dalinamas is 16 ir randama liekana is padalinto skaiciaus
  29. liekana = sveikaDalis % 16;
  30. sveikaDalis /= 16;
  31. if (liekana < 10) hex.insert(0, to_string(liekana));
  32. else if (liekana == 10) hex.insert(0, "A");
  33. else if (liekana == 11) hex.insert(0, "B");
  34. else if (liekana == 12) hex.insert(0, "C");
  35. else if (liekana == 13) hex.insert(0, "D");
  36. else if (liekana == 14) hex.insert(0, "E");
  37. else if (liekana == 15) hex.insert(0, "F");
  38. }
  39. return hex;
  40. }
  41.  
  42. string hexToBin(string sk) {
  43. string bin;
  44. for (int i = 0; i < sk.length(); i++) {
  45. if (sk[i] == '0') bin.append("0000");
  46. else if (sk[i] == '1') bin.append("0001");
  47. else if (sk[i] == '2') bin.append("0010");
  48. else if (sk[i] == '3') bin.append("0011");
  49. else if (sk[i] == '4') bin.append("0100");
  50. else if (sk[i] == '5') bin.append("0101");
  51. else if (sk[i] == '6') bin.append("0110");
  52. else if (sk[i] == '7') bin.append("0111");
  53. else if (sk[i] == '8') bin.append("1000");
  54. else if (sk[i] == '9') bin.append("1001");
  55. else if (sk[i] == 'A') bin.append("1010");
  56. else if (sk[i] == 'B') bin.append("1011");
  57. else if (sk[i] == 'C') bin.append("1100");
  58. else if (sk[i] == 'D') bin.append("1101");
  59. else if (sk[i] == 'E') bin.append("1110");
  60. else if (sk[i] == 'F') bin.append("1111");
  61. }
  62. return bin;
  63. }
  64.  
  65. string binToHex(string unicode) {
  66. string utf;
  67. for (int i = 0; i < unicode.length(); i += 4) { //paimami 4 skaiciai is ivesto skaiciaus ir prilyginami
  68. string ch;
  69. ch = unicode.substr(i, 4);
  70. if (ch == "0000") utf.append("0");
  71. else if (ch == "0001") utf.append("1");
  72. else if (ch == "0010") utf.append("2");
  73. else if (ch == "0011") utf.append("3");
  74. else if (ch == "0100") utf.append("4");
  75. else if (ch == "0101") utf.append("5");
  76. else if (ch == "0110") utf.append("6");
  77. else if (ch == "0111") utf.append("7");
  78. else if (ch == "1000") utf.append("8");
  79. else if (ch == "1001") utf.append("9");
  80. else if (ch == "1010") utf.append("A");
  81. else if (ch == "1011") utf.append("B");
  82. else if (ch == "1100") utf.append("C");
  83. else if (ch == "1101") utf.append("D");
  84. else if (ch == "1110") utf.append("E");
  85. else if (ch == "1111") utf.append("F");
  86. }
  87. return utf;
  88. }
  89.  
  90.  
  91. string convert(int level, string bin) {
  92. string utf;
  93. while (1) {
  94. if (bin.length() > 0) {
  95. utf.insert(0, 1, bin.back()); //imama paskutinis string char
  96. bin = bin.substr(0, bin.size() - 1); //nutrinamas paskutinis char
  97. }
  98.  
  99. else utf.insert(0, "0");
  100.  
  101. if (utf.length() == 13 && level == 2) {
  102. utf.insert(0, "110");
  103. break;
  104. }
  105. else if (utf.length() == 20 && level == 3) {
  106. utf.insert(0, "1110");
  107. break;
  108. }
  109. else if (utf.length() == 27 && level == 4) {
  110. utf.insert(0, "11110");
  111. break;
  112. }
  113.  
  114. if (utf.length() == 6 || utf.length() == 14 || utf.length() == 22) utf.insert(0, "10");
  115.  
  116. }
  117. return utf;
  118. }
  119.  
  120. void library(map<int, int> &lib) {
  121. ifstream fd("librarynochar.txt");
  122. string blank;
  123. int unicode;
  124. int codePage;
  125. unsigned char ch;
  126. for (int i = 128; i < 256; i++) {
  127. fd >> blank >> codePage >> unicode;
  128.  
  129. lib[codePage] = unicode;
  130. }
  131. }
  132.  
  133. void hexToFullChar(string a) {
  134.  
  135. for (int i = 0; i < a.length(); i+=2) {
  136. int b = 0;
  137. if (a[i] >= '0' && a[i] <= '9') {
  138. b += (a[i] - '0') * 16;
  139. }
  140. else {
  141. b += (a[i] - 'A' + 10) * 16;
  142. }
  143.  
  144. if (a[i+1] >= '0' && a[i+1] <= '9') {
  145. b += (a[i+1] - '0');
  146. }
  147. else {
  148. b += (a[i+1] - 'A' + 10);
  149. }
  150.  
  151. char ch = b;
  152. fr << ch;
  153.  
  154. }
  155. }
  156.  
  157.  
  158. int main() {
  159.  
  160. map<int, int> lib;
  161. library(lib);
  162.  
  163. ifstream fd("386intel.txt");
  164.  
  165. fd >> noskipws; //kad skaitytu ir- space
  166.  
  167. while (!fd.eof()) {
  168. unsigned char ch;
  169. fd >> ch;
  170. int raide = ch;
  171.  
  172. if (raide < 128) {
  173. fr << ch;
  174. }
  175. else {
  176. raide = lib[raide];
  177. string hex = decToHex(raide);
  178. string bin = hexToBin(hex);
  179. string utf;
  180. if (raide < 2048) {
  181. utf = binToHex(convert(2, bin));
  182. }
  183. else if (raide < 65536) {
  184. utf = binToHex(convert(3, bin));
  185. }
  186. else {
  187. utf = binToHex(convert(4, bin));
  188. }
  189. hexToFullChar(utf);
  190.  
  191.  
  192. }
  193. }
  194.  
  195. return 0;
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement