#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=; i<nLen; i++)
{
h1 = pbSrc[*i];
h2 = pbSrc[*i+]; s1 = toupper(h1) - 0x30;
if (s1 > )
s1 -= ; s2 = toupper(h2) - 0x30;
if (s2 > )
s2 -= ; pbDest[i] = s1* + 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=; i<nLen; i++)
{
ddh = + pbSrc[i] / ;
ddl = + pbSrc[i] % ;
if (ddh > ) ddh = ddh + ;
if (ddl > ) ddl = ddl + ;
pbDest[i*] = ddh;
pbDest[i*+] = ddl;
} pbDest[nLen*] = '\0';
} int RS232_SendBuf(int comport_number,unsigned char *data_buf,int size)
{
char str[] = "";
HexToStr(str, data_buf, size);
printf("str == %s\n", str);
return ;
} 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 = -;
unsigned char data_buf[]; switch(cmd)
{
case 0x50:
// data_buf = (unsigned char*)malloc((size+7)*sizeof(unsigned char));
data_buf[] = 0XA0;
data_buf[] = 0X90|((cardID>>)&0x0F);
data_buf[] =(unsigned char)(cardID&0x00ff);
data_buf[] = 0x50;
data_buf[] = size/;
data_buf[] = size%;
if(size != )
{
for(i = ;i<size;i++)
{
data_buf[+i] = buf[i];
}
}
else
{
data_buf[] = ;
}
data_buf[size+] = data_buf[]; for( i = ;i<size+;i++)
{
data_buf[size+] = data_buf[i]^data_buf[size+];
}
satae= RS232_SendBuf(comport_number,data_buf,size+);
break;
case 0xA5:
// data_buf = (unsigned char*)malloc((size+8)*sizeof(unsigned char));
data_buf[] = 0XA0;
data_buf[] = 0X90|((cardID>>)&0x0F);
data_buf[] =(unsigned char)(cardID&0x00ff);
data_buf[] = 0xA5;
data_buf[] = size/;
data_buf[] = size%;
if(size != )
{
for( i = ;i<size;i++)
{
data_buf[+i] = buf[i];
}
}
else
{
data_buf[] = ;
}
data_buf[size+] = data_buf[];
for( i = ;i<size+;i++)
{
data_buf[size+] = data_buf[i]^data_buf[size+];
}
satae= RS232_SendBuf(comport_number,data_buf,size+);
break;
case 0x58:
// data_buf = (unsigned char*)malloc((size+8)*sizeof(unsigned char));
data_buf[] = 0XA0;
data_buf[] = 0X90|((cardID>>)&0x0F);
data_buf[] =(unsigned char)(cardID&0x00ff);
data_buf[] = 0x58;
if(size == )
{
data_buf[] = ;
data_buf[] = ;
data_buf[] = data_buf[]^data_buf[]^data_buf[]^data_buf[];
satae = RS232_SendBuf(comport_number,data_buf,);
}
else
{
data_buf[] = ;
data_buf[] = ;
data_buf[] = 0xFF;
data_buf[] = data_buf[]^data_buf[]^data_buf[]^data_buf[]^data_buf[];
satae = RS232_SendBuf(comport_number,data_buf,);
}
break;
case 0x48:
data_buf[] = 0XA0;
data_buf[] = 0X90|((cardID>>)&0x0F);
data_buf[] =(unsigned char)(cardID&0x00ff);
data_buf[] = 0x48;
data_buf[] = ;
data_buf[] = ;
data_buf[] = cmd_Sxt;
data_buf[] = data_buf[]^data_buf[]^data_buf[]^data_buf[]^data_buf[];
satae = RS232_SendBuf(comport_number,data_buf,);
break;
case 0x49:
// data_buf = (unsigned char*)malloc((size+9)*sizeof(unsigned char));
data_buf[] = 0XA0;
data_buf[] = 0X90|((cardID>>)&0x0F);
data_buf[] =(unsigned char)(cardID&0x00ff);
data_buf[] = 0x49;
data_buf[] = (size+)/;
data_buf[] = (size+)%;
if(size != )
{
data_buf[] = cmd_Sxt;
for(i = ;i<size;i++)
{
data_buf[+i] = buf[i];
}
}
else
{
data_buf[] = ;
}
data_buf[size+] = data_buf[];
for(i = ;i<size+;i++)
{
data_buf[size+] = data_buf[i]^data_buf[size+];
}
satae = RS232_SendBuf(comport_number,data_buf,size+);
break;
case 0x44:
// data_buf = (unsigned char*)malloc((size+8)*sizeof(unsigned char));
data_buf[] = 0XA0;
data_buf[] = 0X90|((cardID>>)&0x0F);
data_buf[] =(unsigned char)(cardID&0x00ff);
data_buf[] = 0x44;
data_buf[] = size/;
data_buf[] = size%;
if(size != )
{
for(int i = ;i<size;i++)
{
data_buf[+i] = buf[i];
}
}
else
{
data_buf[] = ;
}
data_buf[size+] = data_buf[]; for( i = ;i<size+;i++)
{
data_buf[size+] = data_buf[i]^data_buf[size+];
}
satae = RS232_SendBuf(comport_number,data_buf,size+);
break;
case 0x51:
// data_buf = (unsigned char*)malloc((size+8)*sizeof(unsigned char));
data_buf[] = 0XA0;
data_buf[] = 0X90|((cardID>>)&0x0F);
data_buf[] =(unsigned char)(cardID&0x00ff);
data_buf[] = 0x51;
data_buf[] = size/;
data_buf[] = size%;
if(size != )
{
for( i = ;i<size;i++)
{
data_buf[+i] = buf[i];
}
}
else
{
data_buf[] = ;
}
data_buf[size+] = data_buf[]; for( i = ;i<size+;i++)
{
data_buf[size+] = data_buf[i]^data_buf[size+];
}
satae = RS232_SendBuf(comport_number,data_buf,size+); break;
case 0x52:
// data_buf = (unsigned char*)malloc((size+8)*sizeof(unsigned char));
data_buf[] = 0XA0;
data_buf[] = 0X90|((cardID>>)&0x0F);
data_buf[] =(unsigned char)(cardID&0x00ff);
data_buf[] = 0x52;
data_buf[] = size/;
data_buf[] = size%;
if(size != )
{
for( i = ;i<size;i++)
{
data_buf[+i] = buf[i];
}
}
else
{
data_buf[] = ;
}
data_buf[size+] = data_buf[]; for( i = ;i<size+;i++)
{
data_buf[size+] = data_buf[i]^data_buf[size+];
}
satae = RS232_SendBuf(comport_number,data_buf,size+); 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)-)
{
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 == -)
{
perror ("iconv");
} /* 存放转换后的字符串 */
printf("outbuf=%s\n", outbuf); //存放转换后outbuf剩余的空间
printf("outlen=%d\n", *outlen); int i = ; for (i=; i<(outsize- (*outlen)); i++)
{
//printf("%2c", outbuf[i]);
printf("%x\n", outbuf[i]);
} /* 关闭句柄 */
iconv_close (cd); return ;
} 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)-)
{
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 == -)
{
perror ("iconv");
} /* 存放转换后的字符串 */
printf("outbuf=%s\n", outbuf); //存放转换后outbuf剩余的空间
printf("outlen=%d\n", *outlen); int i = ; for (i=; i<(outsize- (*outlen)); i++)
{
//printf("%2c", outbuf[i]);
printf("%x\n", outbuf[i]);
} /* 关闭句柄 */
iconv_close (cd); return ;
} int main()
{
char *unicode_buf = NULL;
size_t num = ; /* 需要转换的字符串 */
//char inbuf[1024] = "张忠旺";
char *text = "张忠旺"; char inbuf[] = {};
strcpy(inbuf, text);
size_t inlen = strlen(inbuf); /* 存放转换后的字符串 */
char outbuf[] = {};
size_t outlen = ; utf8_to_unicode(inbuf, &inlen, outbuf, &outlen); printf("out_len = %d\n", outlen ); RS232_SxtBuf(, , 0x50, , outbuf + , - outlen - );
return ;
}

utf8转unicode的更多相关文章

  1. UTF-8和Unicode

    What's the difference between unicode and utf8? up vote 103 down vote favorite 49 Is it true that un ...

  2. 【GoLang】GoLang UTF8 与 Unicode

    结论: 通用的UTF8编码可是Ken Thompson和Rob Pike共同发明的, 他们都是Go的作者. Go中rune对应unicode的码点, string只是UTF8编码.len(" ...

  3. [转]unicode,ansi,utf-8,unicode big endian的故事

    unicode,ansi,utf-8,unicode big endian的故事很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们看到8个开关状态是好的 ...

  4. paip.utf-8,unicode编码的本质输出unicode文件原理 python

    paip.utf-8,unicode编码的本质输出unicode文件原理 python      #别的语言,java php都是unicode,走十python不一样.    #enddef  #t ...

  5. PHP UTF-8和Unicode编号互转

    PHP UTF-8和Unicode编号互转 /** * utf-8 转unicode * * @param string $name * @return string */ function utf8 ...

  6. 跨平台utf8转unicode研究实现(2)

    最近在用VC++开发一个小工具,平时用惯了.NET,用起VC++最郁闷的就是字符串处理.当然最最让人难于琢磨的就是字符集,编码之间的转换.通过这几天的研究,终于明白了Unicode和UTF-8之间编码 ...

  7. 浅谈编码Base64、Hex、UTF-8、Unicode、GBK等

    网络上大多精彩的回答,该随笔用作自我总结: 首先计算机只认得二进制,0和1,所以我们现在看到的字都是经过二进制数据编码后的:计算机能针对0和1的组合做很多事情,这些规则都是人定义的:然后有了字节的概念 ...

  8. linux下php中文UTF-8转换Unicode方法和注意事项

    先说下遇到问题:1.php没有内置unicode_ecode函数可以直接使用 2.网上很多资料都是用$str = iconv($encoding, 'UCS-2', $str); window下转换出 ...

  9. utf-8和Unicode的区别

    链接 utf-8和Unicode到底有什么区别?是存储方式不同?编码方式不同?它们看起来似乎很相似,但是实际上他们并不是同一个层次的概念 要想先讲清楚他们的区别,首先应该讲讲Unicode的来由. 众 ...

  10. Python中GBK, UTF-8和Unicode的编码问题

    编码问题,一直是使用python2时的一块心病.几乎所有的控制台输入输出.IO操作和HTTP操作都会涉及如下的编码问题: UnicodeDecodeError:‘ascii’codec can’t d ...

随机推荐

  1. python 换行

    python3 end = “”:输入参数不换行. 就是打印之后不换行. python字符串换行: (1)三个单引号,例如print '''我是程序员 刚学习python''' (2)三个双引号,例如 ...

  2. 使用ES6的Promise 解决回调函数。

    //创建一个Promise实例,获取数据.并把数据传递给处理函数resolve和reject.需要注意的是Promise在声明的时候就执行了. var getUserInfo=new Promise( ...

  3. Disruptor_学习_00_资源帖

    一.官方 disruptor-github disruptor-api LMAX Disruptor 二.精选资料 Disruptor入门-官方文档翻译-方腾飞 Disruptor官方文档实现 Dis ...

  4. @angular/cli项目构建--http(2)

    客户端GET设置参数查询: search() { const params = new HttpParams() .set('userName', this.userName) .set('fullN ...

  5. OpenCV - opencv3 图像处理 之 图像缩放( python与c++实现 )

    转自:https://www.cnblogs.com/dyufei/p/8205121.html 一. 主要函数介绍 1) 图像大小变换 cvResize () 原型: voidcvResize(co ...

  6. java多线程 生产者消费者案例-虚假唤醒

    package com.java.juc; public class TestProductAndConsumer { public static void main(String[] args) { ...

  7. [SP16549]QTREE6

    luogu vjudge 题意 给你一棵n个点的树,编号1~n.每个点可以是黑色,可以是白色.初始时所有点都是黑色.支持两种操作: 0 u:询问有多少个节点v满足路径u到v上所有节点(包括)都拥有相同 ...

  8. C/C++变量命名规则,个人习惯总结【转载】

    C_C++变量命名规则 原文地址:http://blog.sina.com.cn/s/blog_8a7012cf01017h9p.html 变量命名规则是为了增强代码的可读性和容易维护性.以下为C++ ...

  9. laravel count的使用

    rt 学习源头: https://blog.csdn.net/chajinglong/article/details/51954010 四.聚合 查询构建器还提供了各种聚合方法,如统计,马克斯,min ...

  10. Python学习笔记 - PostgreSQL的使用

    一.安装PostgreSQL模块 pip install psycopg2 有时候会失败,多安装2次就好了(我是第二次成功了). 二.数据库连接接口 由于Python统一了数据库连接的接口,所以psy ...