Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum encode_mode {
- UTFtoWIN,
- WINtoUTF
- };
- std::string encoder(std::string text_to_encode, int encode_mode) {
- BSTR bstrWide;
- int length;
- char *text = new char[text_to_encode.size() + 1];
- for (int i = 0; i < text_to_encode.size(); i++) {
- text[i] = text_to_encode[i];
- }
- text[text_to_encode.size()] = '\0';
- if (encode_mode == encode_mode::UTFtoWIN) {
- length = MultiByteToWideChar(CP_UTF8, 0, text, strlen(text), NULL, NULL);
- bstrWide = SysAllocStringLen(NULL, length);
- MultiByteToWideChar(CP_UTF8, 0, text, strlen(text), bstrWide, length);
- length = WideCharToMultiByte(1251, 0, bstrWide, -1, NULL, 0, NULL, NULL);
- char *text_1251 = new char[length];
- WideCharToMultiByte(1251, 0, bstrWide, -1, text_1251, length, NULL, NULL);
- SysFreeString(bstrWide);
- text_to_encode.clear();
- for (int i = 0; i < strlen(text_1251); i++) {
- text_to_encode += text_1251[i];
- }
- delete[]text_1251;
- }
- else if (encode_mode == encode_mode::WINtoUTF) {
- length = MultiByteToWideChar(1251, 0, text, strlen(text), NULL, NULL);
- bstrWide = SysAllocStringLen(NULL, length);
- MultiByteToWideChar(1251, 0, text, strlen(text), bstrWide, length);
- length = WideCharToMultiByte(CP_UTF8, 0, bstrWide, -1, NULL, 0, NULL, NULL);
- char *text_u8 = new char[length];
- WideCharToMultiByte(CP_UTF8, 0, bstrWide, -1, text_u8, length, NULL, NULL);
- SysFreeString(bstrWide);
- text_to_encode.clear();
- for (int i = 0; i < strlen(text_u8); i++) {
- text_to_encode += text_u8[i];
- }
- delete[]text_u8;
- }
- delete[]text;
- return text_to_encode;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement