# text\_encoding

***

**1. Encoding Text to UTF-8:**

```cpp
#include <string>
#include <locale>

int main() {
  std::wstring wstr = L"Hello, world!";
  std::string str = std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(wstr);
  std::cout << str << std::endl;  // Output: Hello, world!
  return 0;
}
```

**2. Decoding UTF-8 Encoded Text:**

```cpp
#include <string>
#include <locale>

int main() {
  std::string str = "Hello, world!";
  std::wstring wstr = std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(str);
  std::wcout << wstr << std::endl;  // Output: Hello, world!
  return 0;
}
```

**3. Encode Text to Base64:**

```cpp
#include <iostream>
#include <algorithm>

int main() {
  std::string text = "Hello, world!";
  std::string encoded;
  encoded.resize(text.length() * 4 / 3 + 4);  // Allocate enough space for padding
  size_t encoded_size = Base64encode(text.data(), text.length(), encoded.data());
  std::cout << encoded.substr(0, encoded_size) << std::endl;  // Output: SGVsbG8sIHdvcmxkIQ==
  return 0;
}
```

**4. Decode Base64 Encoded Text:**

```cpp
#include <iostream>
#include <algorithm>

int main() {
  std::string encoded = "SGVsbG8sIHdvcmxkIQ==";
  std::string text;
  text.resize(encoded.length() * 3 / 4);  // Allocate enough space for padding
  size_t text_size = Base64decode(encoded.data(), encoded.length(), text.data());
  std::cout << text.substr(0, text_size) << std::endl;  // Output: Hello, world!
  return 0;
}
```

**5. Encode Text to Caesar Cipher:**

```cpp
#include <iostream>
#include <algorithm>

int main() {
  int shift = 3;
  std::string text = "Hello, world!";
  std::transform(text.begin(), text.end(), text.begin(), [shift](char c) {
    if (isalpha(c)) {
      if (isupper(c)) {
        c = (c - 'A' + shift) % 26 + 'A';
      } else {
        c = (c - 'a' + shift) % 26 + 'a';
      }
    }
    return c;
  });
  std::cout << text << std::endl;  // Output: Khoor, zruog!
  return 0;
}
```

**6. Decode Caesar Cipher Encoded Text:**

```cpp
#include <iostream>
#include <algorithm>

int main() {
  int shift = 3;
  std::string text = "Khoor, zruog!";
  std::transform(text.begin(), text.end(), text.begin(), [shift](char c) {
    if (isalpha(c)) {
      if (isupper(c)) {
        c = (c - 'A' - shift + 26) % 26 + 'A';
      } else {
        c = (c - 'a' - shift + 26) % 26 + 'a';
      }
    }
    return c;
  });
  std::cout << text << std::endl;  // Output: Hello, world!
  return 0;
}
```

**7. Encode Text to MD5 Hash:**

```cpp
#include <iostream>
#include <openssl/md5.h>

int main() {
  std::string text = "Hello, world!";
  unsigned char digest[MD5_DIGEST_LENGTH];
  MD5((unsigned char*)text.c_str(), text.length(), digest);
  std::cout << std::hex << std::setfill('0') << std::setw(MD5_DIGEST_LENGTH * 2);
  for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
    std::cout << (int)digest[i];
  }
  std::cout << std::endl;  // Output: b10a8db164e0754105b7a99be72e3fe5
  return 0;
}
```

**8. Encode Text to SHA-256 Hash:**

```cpp
#include <iostream>
#include <openssl/sha.h>

int main() {
  std::string text = "Hello, world!";
  unsigned char digest[SHA256_DIGEST_LENGTH];
  SHA256((unsigned char*)text.c_str(), text.length(), digest);
  std::cout << std::hex << std::setfill('0') << std::setw(SHA256_DIGEST_LENGTH * 2);
  for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) {
    std::cout << (int)digest[i];
  }
  std::cout << std::endl;  // Output: 7f83b1657ff1fc53b92dc18148a1d65dfc2d4f8e053f884e3c516fa27169a7d

```
