📄 encode.cpp
字号:
/* encode.cpp -- code convert function * Designed by arthur, 2009-4-24 * */#include <iconv.h>#include <string.h>#include <errno.h>#include <iostream>#include <stdlib.h>#include <fstream>int bufferleft;char inputbuffer[10240000];char outputbuffer[10740000];using namespace std;char *code_knowned[] = { /*define you own codes here*/"utf-8", "gbk", "gb2312", "gb18030" };int code_convert(char *from_code, char *to_code, char *from, char *to) /*code from one to another*/{ static iconv_t cd; static char *tptr, *fptr; static size_t ileft, oleft, ret; tptr = fptr =NULL; if (from == NULL || to == NULL) return -1; cd = iconv_open((const char *)to_code, (const char *)from_code); if (cd == (iconv_t)-1) { return -1; } ileft = strlen(from); fptr = from; for (;;) { tptr = to; oleft = 10740000; ret = iconv(cd, &fptr, &ileft, &tptr, &oleft); if (ret != (size_t)-1) { break; } if (errno == EINVAL) { (void) memmove(from, fptr, ileft); (void) iconv(cd, &fptr, &ileft, &tptr, &oleft); (void) iconv_close(cd); bufferleft = oleft; return 1; } else if (errno == E2BIG) { continue; } else if (errno == EILSEQ) { (void) iconv(cd, &fptr, &ileft, &tptr, &oleft); (void) iconv_close(cd); bufferleft = oleft; return 1; } else if (errno == EBADF) { return -1; } else { return -1; } } (void) iconv(cd, &fptr, &ileft, &tptr, &oleft); bufferleft = oleft; cout<< bufferleft <<endl; // cout<< oleft<<endl; (void) iconv_close(cd); return 0;}/*convert any code to the code you want*/int code_convert_to(char *to_code, char *from, char *to) { static int i, ret, code_knowned_num; code_knowned_num = sizeof(code_knowned) / sizeof(code_knowned[0]); for (i = 0; i < code_knowned_num; i++) { ret = code_convert(code_knowned[i], to_code, from, to); if (ret == 0) return 0; else if (ret == 1) continue; else { return -1; } } return 0;}/*convert any code to unicode*/char * c2utf8(char *from, char *to) { static int ret; ret = code_convert_to("utf-8", from, to); if (ret == -1) { return NULL; } else { return to; }}int main() { fstream fin; fstream fout; char infile[100]; char outfile[100]; cout<<"Please enter the input file name:"<<endl; cin >> infile; fin.open(infile, ios::in); cout<<"Please enter the output file name:"<<endl; cin >> outfile; fout.open(outfile, ios::out|ios::trunc); bzero(inputbuffer, sizeof(inputbuffer)); fin.read(inputbuffer, sizeof(inputbuffer)); c2utf8(inputbuffer, outputbuffer); fout.write(outputbuffer, sizeof(outputbuffer) - bufferleft); fin.close(); fout.close(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -