First of all, I just want to use Baltic characters in console AND execute CMD commands with them but problem start with default/standart console c++ application.
#include <iostream>
int main() {
string output = "āāāčččēēēē";
cout << output << endl;
}
Earlier, I made this question on stack - How to use UTF8 characters in DEFAULT c++ project OR when using mysql connector for c++ in visual studio 2019 (Latin7_general_ci to UTF-8)?
What I discovered in testing: If I convert UTF8 string to Latin1 string, then cout or print hex values, I get some special characters to be outputted in console. For example -
**char s2[256] = "\xc3\xa9";** printed is outputted as "ķ" THAT MEANS I need to convert strings into correct HEX values when it is needed, and some people might know how it might be one.
BUT MY CODE LOGIC needs a feature TO USE THIS STRING TO use cp in CMD. So converting to string later, fails my CMD to work, although the output of the cp command CMD has to execute seems to show correctly in console.
// Example program
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <cstring>
#include <cstdint>
#include <locale>
#include <cstdlib>
int GetUtf8CharacterLength(unsigned char utf8Char)
{
if (utf8Char < 0x80) return 1;
else if ((utf8Char & 0x20) == 0) return 2;
else if ((utf8Char & 0x10) == 0) return 3;
else if ((utf8Char & 0x08) == 0) return 4;
else if ((utf8Char & 0x04) == 0) return 5;
return 6;
}
char Utf8ToLatin1Character(char* s, int* readIndex)
{
int len = GetUtf8CharacterLength(static_cast<unsigned char>(s[*readIndex]));
if (len == 1)
{
char c = s[*readIndex];
(*readIndex)++;
return c;
}
unsigned int v = (s[*readIndex] & (0xff >> (len + 1))) << ((len - 1) * 6);
(*readIndex)++;
for (len--; len > 0; len--)
{
v |= (static_cast<unsigned char>(s[*readIndex]) - 0x80) << ((len - 1) * 6);
(*readIndex)++;
}
return (v > 0xff) ? 0 : (char)v;
}
// overwrites s in place
char* Utf8ToLatin1String(char* s)
{
for (int readIndex = 0, writeIndex = 0; ; writeIndex++)
{
if (s[readIndex] == 0)
{
s[writeIndex] = 0;
break;
}
char c = Utf8ToLatin1Character(s, &readIndex);
if (c == 0)
{
c = '_';
}
s[writeIndex] = c;
}
return s;
}
int main()
{
char s2[256] = "\xc3\xa9";
Utf8ToLatin1String(s2);
std::cout << s2 << std::endl;
std::string locations2 = ("C:\\Users\\Janis\\Desktop\\TEST2\\");
std::string txtt = (".txt");
std::string copy2 = ("copy /-y ");
std::string space = " ";
std::string PACIENTI2 = "C:\\PACIENTI\\";
std::string element = copy2 + locations2 + s2 + txtt;
std::string cmd = element + space + PACIENTI2 + s2 + txtt;
std::cout << cmd << std::endl;
FILE* pipe = _popen(cmd.c_str(), "r");
}
So we need to really solve two problems, creating hex string from already given, and making sure it works in CMD.