title: Encoding Vietnamese characters from ISO88591, UTF8, UTF16BE, UTF16LE, UTF16 to Hex and vice versa using C++
body: <p>I have edited my post. Currently what I'm trying to do is to encode an input string from the user and then convert it to Hex formats. I can do it properly if it does not contain any Vietnamese character.
  If my inputString is "Hello". But when I try to input a string such as "Tôi", I don't know how to do it. </p>
  
  <pre><code>    enum Encodings { USASCII, ISO88591, UTF8, UTF16BE, UTF16LE, UTF16, BIN, OCT, HEX };
  
      switch (Encodings)
          {
          case USASCII:
              ASCIIToHex(inputString, &amp;ascii); //hello output 48656C6C6F
              return new ByteField(ascii.c_str());
          case ISO88591:
              ASCIIToHex(inputString, &amp;ascii);//hello output 48656C6C6F
                                                //tôi output 54F469
              return new ByteField(ascii.c_str());
          case UTF8:
              ASCIIToHex(inputString, &amp;ascii);//hello output 48656C6C6F
                                             //tôi output 54C3B469
              return new ByteField(ascii.c_str());
          case UTF16BE:
              ToUTF16(inputString, &amp;ascii, Encodings);//hello output 00480065006C006C006F
                                                      //tôi output 005400F40069
              return new ByteField(ascii.c_str());
          case UTF16:
          ToUTF16(inputString, &amp;ascii, Encodings);//hello output FEFF00480065006C006C006F
                                                  //tôi output FEFF005400F40069
              return new ByteField(ascii.c_str());
          case UTF16LE:
              ToUTF16(inputString, &amp;ascii, Encodings);//hello output 480065006C006C006F00
                                                      //tôi output 5400F4006900
              return new ByteField(ascii.c_str());
          }
  
  void StringUtilLib::ASCIIToHex(std::string s, std::string * result)
  {
      int n = s.length();
      for (int i = 0; i &lt; n; i++)
      {
          unsigned char c = s[i];
          long val = long(c);
          std::string bin = "";
          while (val &gt; 0)
          {
              (val % 2) ? bin.push_back('1') :
                  bin.push_back('0');
              val /= 2;
          }
          reverse(bin.begin(), bin.end());
          result-&gt;append(ConvertBinToHex(bin));
      }
  }
  
  std::string ToUTF16(std::string s, std::string * result, int encodings) {
      int n = s.length();
      if (encodings == UTF16) {
          result-&gt;append("FEFF");
      }
      for (int i = 0; i &lt; n; i++)
      {
          int val = int(s[i]);
          std::string bin = "";
          while (val &gt; 0)
          {
              (val % 2) ? bin.push_back('1') :
                  bin.push_back('0');
              val /= 2;
          }
          reverse(bin.begin(), bin.end());
          if (encodings == UTF16 || encodings == UTF16BE) {
              result-&gt;append("00" + ConvertBinToHex(bin));
          }
          if (encodings == UTF16LE) {
              result-&gt;append(ConvertBinToHex(bin) + "00");
          }
  
      }
  }
  
  std::string ConvertBinToHex(std::string str) {
      long long temp = atoll(str.c_str());
      int dec_value = 0;
      int base = 1;
      int i = 0;
      while (temp) {
          int last_digit = temp % 10;
          temp = temp / 10;
          dec_value += last_digit * base;
          base = base * 2;
      }
      char hexaDeciNum[10];
      while (dec_value != 0)
      {
          int temp = 0;
          temp = dec_value % 16;
          if (temp &lt; 10)
          {
              hexaDeciNum[i] = temp + 48;
              i++;
          }
          else
          {
              hexaDeciNum[i] = temp + 55;
              i++;
          }
          dec_value = dec_value / 16;
      }
      str.clear();
      for (int j = i - 1; j &gt;= 0; j--) {
          str = str + hexaDeciNum[j];
      }
      return str;
  }
  </code></pre>