utf8转unicode
2017-02-26 11:24 fdd566 阅读(456) 评论(0) 编辑 收藏 举报#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #include <iconv.h> #include <ctype.h> #define BYTE unsigned char /* // C prototype : void StrToHex(BYTE *pbDest, BYTE *pbSrc, int nLen) // parameter(s): [OUT] pbDest - 输出缓冲区 // [IN] pbSrc - 字符串 // [IN] nLen - 16进制数的字节数(字符串的长度/2) // return value: // remarks : 将字符串转化为16进制数 */ void StrToHex(BYTE *pbDest, BYTE *pbSrc, int nLen) { char h1,h2; BYTE s1,s2; int i; for (i=0; i<nLen; i++) { h1 = pbSrc[2*i]; h2 = pbSrc[2*i+1]; s1 = toupper(h1) - 0x30; if (s1 > 9) s1 -= 7; s2 = toupper(h2) - 0x30; if (s2 > 9) s2 -= 7; pbDest[i] = s1*16 + s2; } } /* // C prototype : void HexToStr(BYTE *pbDest, BYTE *pbSrc, int nLen) // parameter(s): [OUT] pbDest - 存放目标字符串 // [IN] pbSrc - 输入16进制数的起始地址 // [IN] nLen - 16进制数的字节数 // return value: // remarks : 将16进制数转化为字符串 */ void HexToStr(BYTE *pbDest, BYTE *pbSrc, int nLen) { char ddl,ddh; int i; for (i=0; i<nLen; i++) { ddh = 48 + pbSrc[i] / 16; ddl = 48 + pbSrc[i] % 16; if (ddh > 57) ddh = ddh + 7; if (ddl > 57) ddl = ddl + 7; pbDest[i*2] = ddh; pbDest[i*2+1] = ddl; } pbDest[nLen*2] = '\0'; } int RS232_SendBuf(int comport_number,unsigned char *data_buf,int size) { char str[1024] = ""; HexToStr(str, data_buf, size); printf("str == %s\n", str); return 0; } int RS232_SxtBuf(int comport_number,int cardID,unsigned char cmd,unsigned char cmd_Sxt,unsigned char *buf,int size) { //unsigned char data_buf[size+8]; int i; int satae = -1; unsigned char data_buf[64]; switch(cmd) { case 0x50: // data_buf = (unsigned char*)malloc((size+7)*sizeof(unsigned char)); data_buf[0] = 0XA0; data_buf[1] = 0X90|((cardID>>8)&0x0F); data_buf[2] =(unsigned char)(cardID&0x00ff); data_buf[3] = 0x50; data_buf[4] = size/255; data_buf[5] = size%255; if(size != 0) { for(i = 0;i<size;i++) { data_buf[6+i] = buf[i]; } } else { data_buf[6] = 0; } data_buf[size+6] = data_buf[2]; for( i = 3;i<size+6;i++) { data_buf[size+6] = data_buf[i]^data_buf[size+6]; } satae= RS232_SendBuf(comport_number,data_buf,size+7); break; case 0xA5: // data_buf = (unsigned char*)malloc((size+8)*sizeof(unsigned char)); data_buf[0] = 0XA0; data_buf[1] = 0X90|((cardID>>8)&0x0F); data_buf[2] =(unsigned char)(cardID&0x00ff); data_buf[3] = 0xA5; data_buf[4] = size/255; data_buf[5] = size%255; if(size != 0) { for( i = 0;i<size;i++) { data_buf[6+i] = buf[i]; } } else { data_buf[6] = 0; } data_buf[size+6] = data_buf[2]; for( i = 3;i<size+6;i++) { data_buf[size+6] = data_buf[i]^data_buf[size+6]; } satae= RS232_SendBuf(comport_number,data_buf,size+7); break; case 0x58: // data_buf = (unsigned char*)malloc((size+8)*sizeof(unsigned char)); data_buf[0] = 0XA0; data_buf[1] = 0X90|((cardID>>8)&0x0F); data_buf[2] =(unsigned char)(cardID&0x00ff); data_buf[3] = 0x58; if(size == 0) { data_buf[4] = 0; data_buf[5] = 0; data_buf[6] = data_buf[5]^data_buf[4]^data_buf[3]^data_buf[2]; satae = RS232_SendBuf(comport_number,data_buf,7); } else { data_buf[4] = 0; data_buf[5] = 1; data_buf[6] = 0xFF; data_buf[7] = data_buf[6]^data_buf[5]^data_buf[4]^data_buf[3]^data_buf[2]; satae = RS232_SendBuf(comport_number,data_buf,8); } break; case 0x48: data_buf[0] = 0XA0; data_buf[1] = 0X90|((cardID>>8)&0x0F); data_buf[2] =(unsigned char)(cardID&0x00ff); data_buf[3] = 0x48; data_buf[4] = 0; data_buf[5] = 1; data_buf[6] = cmd_Sxt; data_buf[7] = data_buf[6]^data_buf[5]^data_buf[4]^data_buf[3]^data_buf[2]; satae = RS232_SendBuf(comport_number,data_buf,8); break; case 0x49: // data_buf = (unsigned char*)malloc((size+9)*sizeof(unsigned char)); data_buf[0] = 0XA0; data_buf[1] = 0X90|((cardID>>8)&0x0F); data_buf[2] =(unsigned char)(cardID&0x00ff); data_buf[3] = 0x49; data_buf[4] = (size+1)/255; data_buf[5] = (size+1)%255; if(size != 0) { data_buf[6] = cmd_Sxt; for(i = 0;i<size;i++) { data_buf[7+i] = buf[i]; } } else { data_buf[6] = 0; } data_buf[size+7] = data_buf[2]; for(i = 3;i<size+7;i++) { data_buf[size+7] = data_buf[i]^data_buf[size+7]; } satae = RS232_SendBuf(comport_number,data_buf,size+8); break; case 0x44: // data_buf = (unsigned char*)malloc((size+8)*sizeof(unsigned char)); data_buf[0] = 0XA0; data_buf[1] = 0X90|((cardID>>8)&0x0F); data_buf[2] =(unsigned char)(cardID&0x00ff); data_buf[3] = 0x44; data_buf[4] = size/255; data_buf[5] = size%255; if(size != 0) { for(int i = 0;i<size;i++) { data_buf[6+i] = buf[i]; } } else { data_buf[6] = 0; } data_buf[size+6] = data_buf[2]; for( i = 3;i<size+6;i++) { data_buf[size+6] = data_buf[i]^data_buf[size+6]; } satae = RS232_SendBuf(comport_number,data_buf,size+7); break; case 0x51: // data_buf = (unsigned char*)malloc((size+8)*sizeof(unsigned char)); data_buf[0] = 0XA0; data_buf[1] = 0X90|((cardID>>8)&0x0F); data_buf[2] =(unsigned char)(cardID&0x00ff); data_buf[3] = 0x51; data_buf[4] = size/255; data_buf[5] = size%255; if(size != 0) { for( i = 0;i<size;i++) { data_buf[6+i] = buf[i]; } } else { data_buf[6] = 0; } data_buf[size+6] = data_buf[2]; for( i = 3;i<size+6;i++) { data_buf[size+6] = data_buf[i]^data_buf[size+6]; } satae = RS232_SendBuf(comport_number,data_buf,size+7); break; case 0x52: // data_buf = (unsigned char*)malloc((size+8)*sizeof(unsigned char)); data_buf[0] = 0XA0; data_buf[1] = 0X90|((cardID>>8)&0x0F); data_buf[2] =(unsigned char)(cardID&0x00ff); data_buf[3] = 0x52; data_buf[4] = size/255; data_buf[5] = size%255; if(size != 0) { for( i = 0;i<size;i++) { data_buf[6+i] = buf[i]; } } else { data_buf[6] = 0; } data_buf[size+6] = data_buf[2]; for( i = 3;i<size+6;i++) { data_buf[size+6] = data_buf[i]^data_buf[size+6]; } satae = RS232_SendBuf(comport_number,data_buf,size+7); break; default : break; } //HAL_Delay(5); // free(data_buf); return satae; } bool unicode_to_utf8 (char *inbuf, size_t *inlen, char *outbuf, size_t *outlen) { /* 目的编码, TRANSLIT:遇到无法转换的字符就找相近字符替换 * IGNORE :遇到无法转换字符跳过*/ char *encTo = "UTF-8//IGNORE"; /* 源编码 */ char *encFrom = "UNICODE"; /* 获得转换句柄 *@param encTo 目标编码方式 *@param encFrom 源编码方式 * * */ iconv_t cd = iconv_open (encTo, encFrom); if (cd == (iconv_t)-1) { perror ("iconv_open"); } /* 需要转换的字符串 */ printf("inbuf=%s\n", inbuf); /* 打印需要转换的字符串的长度 */ printf("inlen=%d\n", *inlen); /* 由于iconv()函数会修改指针,所以要保存源指针 */ char *tmpin = inbuf; char *tmpout = outbuf; size_t insize = *inlen; size_t outsize = *outlen; /* 进行转换 *@param cd iconv_open()产生的句柄 *@param srcstart 需要转换的字符串 *@param inlen 存放还有多少字符没有转换 *@param tempoutbuf 存放转换后的字符串 *@param outlen 存放转换后,tempoutbuf剩余的空间 * * */ size_t ret = iconv (cd, &tmpin, inlen, &tmpout, outlen); if (ret == -1) { perror ("iconv"); } /* 存放转换后的字符串 */ printf("outbuf=%s\n", outbuf); //存放转换后outbuf剩余的空间 printf("outlen=%d\n", *outlen); int i = 0; for (i=0; i<(outsize- (*outlen)); i++) { //printf("%2c", outbuf[i]); printf("%x\n", outbuf[i]); } /* 关闭句柄 */ iconv_close (cd); return 0; } bool utf8_to_unicode(char *inbuf, size_t *inlen, char *outbuf, size_t *outlen) { /* 目的编码, TRANSLIT:遇到无法转换的字符就找相近字符替换 * IGNORE :遇到无法转换字符跳过*/ char *encTo = "UNICODE//IGNORE"; /* 源编码 */ char *encFrom = "UTF-8"; /* 获得转换句柄 *@param encTo 目标编码方式 *@param encFrom 源编码方式 * * */ iconv_t cd = iconv_open (encTo, encFrom); if (cd == (iconv_t)-1) { perror ("iconv_open"); } /* 需要转换的字符串 */ printf("inbuf=%s\n", inbuf); /* 打印需要转换的字符串的长度 */ printf("inlen=%d\n", *inlen); /* 由于iconv()函数会修改指针,所以要保存源指针 */ char *tmpin = inbuf; char *tmpout = outbuf; size_t insize = *inlen; size_t outsize = *outlen; /* 进行转换 *@param cd iconv_open()产生的句柄 *@param srcstart 需要转换的字符串 *@param inlen 存放还有多少字符没有转换 *@param tempoutbuf 存放转换后的字符串 *@param outlen 存放转换后,tempoutbuf剩余的空间 * * */ size_t ret = iconv(cd, &tmpin, inlen, &tmpout, outlen); if (ret == -1) { perror ("iconv"); } /* 存放转换后的字符串 */ printf("outbuf=%s\n", outbuf); //存放转换后outbuf剩余的空间 printf("outlen=%d\n", *outlen); int i = 0; for (i=0; i<(outsize- (*outlen)); i++) { //printf("%2c", outbuf[i]); printf("%x\n", outbuf[i]); } /* 关闭句柄 */ iconv_close (cd); return 0; } int main() { char *unicode_buf = NULL; size_t num = 0; /* 需要转换的字符串 */ //char inbuf[1024] = "张忠旺"; char *text = "张忠旺"; char inbuf[1024] = {}; strcpy(inbuf, text); size_t inlen = strlen(inbuf); /* 存放转换后的字符串 */ char outbuf[1024] = {0}; size_t outlen = 1024; utf8_to_unicode(inbuf, &inlen, outbuf, &outlen); printf("out_len = %d\n", outlen ); RS232_SxtBuf(0, 1, 0x50, 0, outbuf + 2, 1024 - outlen - 2); return 0; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步