第二版本:
#include <stdio.h>
#include <stdlib.h>
#include <iconv.h>
#include <errno.h>
#include <string.h>
int main(void)
{
unsigned char *src = "中国A1"; /**//* 需转换的字串 */
unsigned char dst[256] = {0}; /**//* 转换后的内容 */
unsigned char buf[1024] = {0}; /**//* 格式化转换后的字串 */
size_t src_len = strlen(src);
size_t dst_len = sizeof(dst);
int len = 8+2; //4个字符*2等于8,再加上UTF-16的前两个字符
unsigned char *in = src;
unsigned char *out = dst;
iconv_t cd;
int i;
int j;
cd = iconv_open("UTF-16", "GB2312"); /**//* 将GB2312字符集转换为UTF-16字符集 */
if ((iconv_t)-1 == cd)
{
printf("iconv_open error:%s ",strerror(errno));
return -1;
}
printf("src: %s,src_len: %d ", src,src_len);
if(iconv(cd, &in, &src_len, &out, &dst_len)<0) /**//* 执行转换 */
{
printf("iconv error:%s ",strerror(errno));
return -1;
}
j = 0;
for (i = 0; i < len; i++)
{
printf("%.02X ", dst[i]);
buf[j++] = '%';
snprintf(buf + j, 3, "%.02X", dst[i]);
j += 2;
}
printf(" ");
if(iconv_close(cd)<0)
{
printf("iconv_close error:%s ",strerror(errno));
return -1;
}
return 0;
}
--------------------next---------------------
阅读(315) | 评论(0) | 转发(0) |