#include
#include
#include
int convert(const char* from_code, const char* to_code, char* from , char* to, int to_len){
if(from == NULL || to == NULL || from_code == NULL || to_code == NULL){
return -1;
}
iconv_t cd = iconv_open(to_code, from_code);
size_t from_len = strlen(from);
size_t dest_len = to_len;
iconv(cd, &from, &from_len, &to, &dest_len);
iconv_close(cd);
return 0;
}
int main(){
char* from = "你好";
char ucs2[10];
memset(ucs2, 0, sizeof(ucs2));
convert("GBK", "UCS2", from, ucs2, sizeof(ucs2));
printf("gbk----->>>>>ucs2 start\n");
int j;
for( j = 0; j < sizeof(ucs2); ++j){
printf("%x,", (unsigned char)ucs2[j]);
}
printf("\nend\n");
char gbk[10];
memset(gbk, 0, sizeof(gbk));
convert("UCS2", "GBK", ucs2, gbk, sizeof(gbk));
printf("ucs----->>>>>gbk start\n");
for( j = 0; j < sizeof(gbk); ++j){
printf("%x,", (unsigned char)gbk[j]);
}
printf("\ngbk string = %s\nend\n", gbk);
}