Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- iconv only works once
- #include <iconv.h>
- #include <iostream>
- #include <stdio.h>
- using namespace std;
- #define BUF_SIZE 1024
- size_t z = (size_t) BUF_SIZE-1;
- bool sjis2utf8( char* text_sjis, char* text_utf8 )
- {
- iconv_t ic;
- ic = iconv_open("UTF8", "SJIS"); // sjis->utf8
- iconv(ic , &text_sjis, &z, &text_utf8, &z);
- iconv_close(ic);
- return true;
- }
- int main(void)
- {
- char hello[BUF_SIZE] = "hello";
- char bye[BUF_SIZE] = "bye";
- char tmp[BUF_SIZE] = "something else";
- sjis2utf8(hello, tmp);
- cout << tmp << endl;
- sjis2utf8(bye, tmp);
- cout << tmp << endl;
- }
- hello
- bye
- hello
- hello
- size_t iconv (iconv_t cd,
- const char* * inbuf, size_t * inbytesleft,
- char* * outbuf, size_t * outbytesleft);
- size_t il = BUF_SIZE - 1 ;
- size_t ol = BUF_SIZE - 1 ;
- iconv(ic, &text_sjis, &il, &text_utf8, &ol) ;
- #include <iconv.h>
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- using namespace std;
- const size_t BUF_SIZE=1024;
- class IConv {
- iconv_t ic_;
- public:
- IConv(const char* to, const char* from)
- : ic_(iconv_open(to,from)) { }
- ~IConv() { iconv_close(ic_); }
- bool convert(char* input, char* output, size_t& out_size) {
- size_t inbufsize = strlen(input)+1;// s-jis string should be null terminated,
- // if s-jis is not null terminated or it has
- // multiple byte chars with null in them this
- // will not work, or to provide in other way
- // input buffer length....
- return iconv(ic_, &input, &inbufsize, &output, &out_size);
- }
- };
- int main(void)
- {
- char hello[BUF_SIZE] = "hello";
- char bye[BUF_SIZE] = "bye";
- char tmp[BUF_SIZE] = "something else";
- IConv ic("UTF8","SJIS");
- size_t outsize = BUF_SIZE;//you will need it
- ic.convert(hello, tmp, outsize);
- cout << tmp << endl;
- outsize = BUF_SIZE;
- ic.convert(bye, tmp, outsize);
- cout << tmp << endl;
- }
- //...
- int len = strlen(text_sjis);
- iconv(ic , &text_sjis, &len, &text_utf8, &z);
- //...
- size_t iconv (iconv_t cd,
- const char* * inbuf, size_t * inbytesleft,
- char* * outbuf, size_t * outbytesleft);
- #include <iconv.h>
- #include <iostream>
- #include <stdio.h>
- #include <string.h>
- using namespace std;
- const size_t BUF_SIZE = 1024;
- size_t z = (size_t) BUF_SIZE-1;
- class IConv {
- iconv_t ic_;
- public:
- IConv(const char* to, const char* from)
- : ic_(iconv_open(to,from)) { }
- ~IConv() { iconv_close(ic_); }
- bool convert(char* input, char* output, size_t outbufsize) {
- size_t inbufsize = strlen(input);
- return iconv(ic_, &input, &inbufsize, &output, &outbufsize);
- }
- };
- int main(void)
- {
- char hello[BUF_SIZE] = "hello";
- char bye[BUF_SIZE] = "bye";
- char tmp[BUF_SIZE] = "something else";
- IConv ic("UTF8","SJIS");
- ic.convert(hello, tmp, BUF_SIZE);
- cout << tmp << endl;
- ic.convert(bye, tmp, BUF_SIZE);
- cout << tmp << endl;
- }
Add Comment
Please, Sign In to add comment