Guest User

Untitled

a guest
Aug 14th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. iconv only works once
  2. #include <iconv.h>
  3. #include <iostream>
  4. #include <stdio.h>
  5. using namespace std;
  6.  
  7. #define BUF_SIZE 1024
  8. size_t z = (size_t) BUF_SIZE-1;
  9.  
  10. bool sjis2utf8( char* text_sjis, char* text_utf8 )
  11. {
  12. iconv_t ic;
  13. ic = iconv_open("UTF8", "SJIS"); // sjis->utf8
  14. iconv(ic , &text_sjis, &z, &text_utf8, &z);
  15. iconv_close(ic);
  16. return true;
  17. }
  18. int main(void)
  19. {
  20. char hello[BUF_SIZE] = "hello";
  21. char bye[BUF_SIZE] = "bye";
  22. char tmp[BUF_SIZE] = "something else";
  23.  
  24. sjis2utf8(hello, tmp);
  25. cout << tmp << endl;
  26.  
  27. sjis2utf8(bye, tmp);
  28. cout << tmp << endl;
  29. }
  30.  
  31. hello
  32. bye
  33.  
  34. hello
  35. hello
  36.  
  37. size_t iconv (iconv_t cd,
  38. const char* * inbuf, size_t * inbytesleft,
  39. char* * outbuf, size_t * outbytesleft);
  40.  
  41. size_t il = BUF_SIZE - 1 ;
  42. size_t ol = BUF_SIZE - 1 ;
  43.  
  44. iconv(ic, &text_sjis, &il, &text_utf8, &ol) ;
  45.  
  46. #include <iconv.h>
  47. #include <iostream>
  48. #include <stdio.h>
  49. #include <string.h>
  50.  
  51. using namespace std;
  52.  
  53. const size_t BUF_SIZE=1024;
  54.  
  55.  
  56. class IConv {
  57. iconv_t ic_;
  58. public:
  59. IConv(const char* to, const char* from)
  60. : ic_(iconv_open(to,from)) { }
  61. ~IConv() { iconv_close(ic_); }
  62.  
  63. bool convert(char* input, char* output, size_t& out_size) {
  64. size_t inbufsize = strlen(input)+1;// s-jis string should be null terminated,
  65. // if s-jis is not null terminated or it has
  66. // multiple byte chars with null in them this
  67. // will not work, or to provide in other way
  68. // input buffer length....
  69. return iconv(ic_, &input, &inbufsize, &output, &out_size);
  70. }
  71. };
  72.  
  73. int main(void)
  74. {
  75. char hello[BUF_SIZE] = "hello";
  76. char bye[BUF_SIZE] = "bye";
  77. char tmp[BUF_SIZE] = "something else";
  78. IConv ic("UTF8","SJIS");
  79.  
  80. size_t outsize = BUF_SIZE;//you will need it
  81. ic.convert(hello, tmp, outsize);
  82. cout << tmp << endl;
  83.  
  84. outsize = BUF_SIZE;
  85. ic.convert(bye, tmp, outsize);
  86. cout << tmp << endl;
  87. }
  88.  
  89. //...
  90. int len = strlen(text_sjis);
  91. iconv(ic , &text_sjis, &len, &text_utf8, &z);
  92. //...
  93.  
  94. size_t iconv (iconv_t cd,
  95. const char* * inbuf, size_t * inbytesleft,
  96. char* * outbuf, size_t * outbytesleft);
  97.  
  98. #include <iconv.h>
  99. #include <iostream>
  100. #include <stdio.h>
  101. #include <string.h>
  102.  
  103. using namespace std;
  104.  
  105. const size_t BUF_SIZE = 1024;
  106. size_t z = (size_t) BUF_SIZE-1;
  107.  
  108. class IConv {
  109. iconv_t ic_;
  110. public:
  111. IConv(const char* to, const char* from)
  112. : ic_(iconv_open(to,from)) { }
  113.  
  114. ~IConv() { iconv_close(ic_); }
  115.  
  116. bool convert(char* input, char* output, size_t outbufsize) {
  117. size_t inbufsize = strlen(input);
  118. return iconv(ic_, &input, &inbufsize, &output, &outbufsize);
  119. }
  120. };
  121.  
  122. int main(void)
  123. {
  124. char hello[BUF_SIZE] = "hello";
  125. char bye[BUF_SIZE] = "bye";
  126. char tmp[BUF_SIZE] = "something else";
  127. IConv ic("UTF8","SJIS");
  128.  
  129.  
  130. ic.convert(hello, tmp, BUF_SIZE);
  131. cout << tmp << endl;
  132.  
  133. ic.convert(bye, tmp, BUF_SIZE);
  134. cout << tmp << endl;
  135. }
Add Comment
Please, Sign In to add comment