www.zhblog.net

C编码转换

c语言字符集转换库:libiconv


1.将字符串由UTF8转为GBK

#include <stdio.h>
#include <iconv.h>

#define ARRAY_SIZE 16


int main() {
const char *fromcode = "UTF8";
const char *tocode = "GBK";
iconv_t it = iconv_open(tocode, fromcode);
char ins[ARRAY_SIZE] = "中国";
char ous[ARRAY_SIZE];
char *insp = ins;
char *ousp = ous;
size_t ist = sizeof(ins);
size_t ost = ARRAY_SIZE * 4;
size_t count = iconv(it, &insp, &ist, &ousp, &ost);

if ((size_t) -1 == count) {
perror("iconv");
}
iconv_close(it);
printf("str %s from %s to %s: %s\n", ins, fromcode, tocode, ous);

for (int i = 0; i < ARRAY_SIZE; i++) {
printf("%x\n", ous[i]);
}
}


结果:

str 中国 from UTF8 to GBK: �й�

ffffffd6

ffffffd0

ffffffb9

fffffffa

0

......


2.将结果由GBK转为UTF8

#include <stdio.h>
#include <iconv.h>

#define ARRAY_SIZE 16


int main() {
const char *fromcode = "GBK";
const char *tocode = "UTF8";
iconv_t it = iconv_open(tocode, fromcode);
char ins[ARRAY_SIZE] = {0xd6, 0xd0, 0xb9, 0xfa};
char ous[ARRAY_SIZE];
char *insp = ins;
char *ousp = ous;
size_t ist = sizeof(ins);
size_t ost = ARRAY_SIZE * 4;
size_t count = iconv(it, &insp, &ist, &ousp, &ost);

if ((size_t) -1 == count) {
perror("iconv");
}
iconv_close(it);
printf("str %s from %s to %s: %s\n", ins, fromcode, tocode, ous);
}


结果:

str �й� from GBK to UTF8: 中国


 

Unicode本身只定义了每个字符的数字编号是多少,并没有规定这个编号如何存储。参考

GBK、UTF8等编码定义了如何进行存储,不同的存储规则相互读取可能会造成乱码。

 

 

 

展开阅读全文

评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 心情