Advertisement
Guest User

Untitled

a guest
Aug 16th, 2018
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. // g++ test.cpp -std=c++14 -Wall
  2. #include <iostream>
  3. #include <cstring>
  4. #include <string>
  5. #include <vector>
  6. #include <locale>
  7.  
  8. using namespace std;
  9.  
  10. class ByteMap
  11. { // Класс проверки кодировки
  12. private:
  13.     char *_data;
  14.     size_t _len;
  15. public:
  16.     ByteMap(const char *data, size_t len) :
  17.             _data(new (std::nothrow) char[len*8]), _len(len*8) {
  18.         const unsigned char mask = 1 << 7;
  19.         if (nullptr == data) {
  20.             std::cerr << "Error ByteMap no init\n";
  21.             return;
  22.         }
  23.         size_t i, z=0;
  24.         for (i = 0; i < len; i++) {
  25.             unsigned char tmp = data[i];
  26.             for (short j = 7; j >= 0; j--) {
  27.                 _data[z++] = (mask&tmp) ? '1' : '0';
  28.                 tmp <<= 1;
  29.             }
  30.         }
  31.     }
  32.  
  33.     ~ByteMap() {
  34.         delete [] _data;
  35.  
  36.     }
  37.     friend std::wostream & operator << (std::wostream &, const ByteMap &);
  38. };
  39.  
  40. std::wostream & operator << (std::wostream &o, const ByteMap &b) {
  41.     for (size_t j = 0; j < b._len; j++) {
  42.         if (!(j % 8) && (j > 0))
  43.             std::wcout << ' ';
  44.         std::wcout << b._data[j];
  45.     }
  46.     return o;
  47. }
  48.  
  49.  
  50. std::wstring _toUtf8(const std::string &str) {
  51.     auto getSymbolLen = [](const char *str, size_t n) {
  52.         if( 0xf0 == (0xf8&str[n]))
  53.             return 4;
  54.         else if( 0xe0 == (0xf0&str[n]))
  55.             return 3;
  56.         else if( 0xc0 == (0xe0&str[n]))
  57.             return 2;
  58.         return 1;
  59.     };
  60.  
  61.     const char *c_str = str.c_str();
  62.     wchar_t wChar;
  63.     char wcharSymbol[sizeof(wchar_t)] = {0};
  64.     std::vector<wchar_t> characters;
  65.  
  66.     for (size_t i = 0; i < str.size();) {
  67.         size_t symbLen = getSymbolLen(str.c_str(), i);
  68.  
  69.         memset(wcharSymbol, 0, sizeof(wchar_t));
  70.         for (size_t j = 0; j < symbLen; ++j)
  71.             wcharSymbol[j] = c_str[i+j];
  72.  
  73.         for (size_t j = 0; j < sizeof(wchar_t); ++j)
  74.             *(((unsigned char*)(&wChar))+j)
  75.                 = wcharSymbol[j];
  76.  
  77.         i += symbLen;
  78.         characters.push_back(wChar);
  79.     }
  80.     return std::wstring(characters.begin(), characters.end());
  81. }
  82.  
  83.  
  84.  
  85. int main() {
  86.     locale::global(std::locale("") );
  87.     wcout.imbue(std::locale());
  88.     cout.imbue(std::locale());
  89.    
  90.     std::string str("добро");
  91.     std::wstring wstr = _toUtf8(str);
  92.     std::wstring wstr2 = L"добро";
  93.    
  94.     std::wcout << wstr << endl;
  95.     std::wcout << wstr2 << endl;
  96.    
  97.     std::wcout << L"wstring_construct\n" << ByteMap((char*)(wstr.c_str()), 5*4) << endl << endl;
  98.     std::wcout << L"string_utf8\n"<< ByteMap((char*)(str.c_str()), 5*4) << endl << endl;
  99.     std::wcout << L"wstring_native\n"  << ByteMap((char*)(wstr2.c_str()), 5*4) << endl << endl;
  100.    
  101.    
  102.     std::cout << str << "\n";
  103.    
  104.    
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement