Visual C++ >> Console
UTF8轉BIG5
參考資訊:
1. utf8_big5
2. iconv_open
main.c
#include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <iconv.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> int main(int argc, char** argv) { int x; iconv_t cd; char in_buf[255]={0}; char out_buf[255]={0}; char *sin=in_buf; char *sout=out_buf; size_t in_len=0; size_t out_len=sizeof(out_buf); cd = iconv_open("big5", "utf-8"); if(cd == -1){ printf("failed to open iconv !\n"); return -1; } int fd = open("test.txt", O_RDONLY); in_len = read(fd, in_buf, sizeof(in_buf)); close(fd); printf("utf8: "); for(x=0; x<in_len; x++){ printf("0x%x ", (unsigned char)in_buf[x]); } printf("\n"); iconv(cd, &sin, &in_len, &sout, &out_len); iconv_close(cd); printf("big5: "); out_len = strlen(out_buf); for(x=0; x<out_len; x++){ printf("0x%x ", (unsigned char)out_buf[x]); } printf("\n"); return 0; }
test.txt
司徒
執行結果
$ ./test utf8: 0xe5 0x8f 0xb8 0xe5 0xbe 0x92 0xa big5: 0xa5 0x71 0xae 0x7b 0xa