fork download
  1. #include <iostream>
  2. #include <locale.h>
  3. #include <iconv.h>
  4. #include <cstring>
  5. #include <cerrno>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. std::string utf8_to_big5(const std::string &str) {
  10. size_t srclen = str.size() + 1, tarlen = str.size() * 2 + 1;
  11. iconv_t cd = iconv_open("BIG5", "UTF8");
  12.  
  13. std::vector<char> cstr(str.c_str(), str.c_str() + str.size() + 1);
  14. cout<<cstr.size()<<endl;
  15. char *src = cstr.data();
  16. char big5buf[str.length() * 2 + 1];
  17. char *dest = big5buf;
  18.  
  19. if (cd == (iconv_t)-1)
  20. throw std::runtime_error("error opening iconv");
  21.  
  22. if (iconv(cd, &src, &srclen, &dest, &tarlen) == -1)
  23. cout<<strerror(errno)<<endl;
  24.  
  25. iconv_close(cd);
  26.  
  27. for (int i = 0; i < strlen(big5buf); i++)
  28. printf("%X\n",big5buf[i]);
  29.  
  30. return std::string(big5buf);
  31. }
  32.  
  33. int main() {
  34. setlocale(LC_ALL,"en_US.utf8");
  35. char utf8buf[] = "好";
  36. if((unsigned char)utf8buf[0] == 0xE5)
  37. cout<<"yes";
  38. cout<<hex
  39.  
  40. <<(int)utf8buf[1]<<endl
  41. <<(int)utf8buf[2]<<endl
  42. <<(int)utf8buf[3]<<endl;
  43. cout<<strlen(utf8buf)<<endl;
  44. //setlocale(LC_ALL,"zh_TW.BIG5");
  45. cout<<utf8_to_big5("好");
  46. return 0;
  47. }
Success #stdin #stdout 0.01s 5516KB
stdin
Standard input is empty
stdout
yesffffffa5
ffffffbd
0
3
4
FFFFFFA6
6E
�n