fork download
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4.  
  5. std::string ConvertCp874ToUtf8(const std::string& str_cp874) {
  6. std::string utf8;
  7. utf8.resize(str_cp874.length() * 3, ' ');
  8. size_t i, j;
  9. for (i = 0, j = 0; i < str_cp874.length(); i++)
  10. {
  11. //guard
  12. if (str_cp874.length() - i >= 2)
  13. if ((str_cp874[i] == 0xE0) &&
  14. (str_cp874[i + 1] == 0xB8 ||
  15. str_cp874[i + 1] == 0xB9)) {
  16. return std::string(); // already converted.
  17. }
  18.  
  19. if (str_cp874[i] < 0xA1) //english
  20. {
  21. utf8[j++] = str_cp874[i];
  22. }
  23. else if (str_cp874[i] < 0xE0) //thai 1
  24. {
  25. utf8[j++] = 0xE0;
  26. utf8[j++] = 0xB8;
  27. utf8[j++] = str_cp874[i] - 0xA0 + 0x80;
  28. }
  29. else //thai 2
  30. {
  31. utf8[j++] = 0xE0;
  32. utf8[j++] = 0xB9;
  33. utf8[j++] = str_cp874[i] - 0xE0 + 0x80;
  34. }
  35. }
  36. utf8.resize(j);
  37. return utf8;
  38. }
  39. int main()
  40. {
  41. std::string name = "\xa1";
  42. auto utf8name = ConvertCp874ToUtf8(name);
  43. for( int i = 0 ;i< 3 ; i++){
  44. printf("%02x " , utf8name[i] );
  45.  
  46. }
  47. }
  48.  
Success #stdin #stdout 0s 4388KB
stdin
Standard input is empty
stdout
ffffffa1 00 20