#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. eclipse部署和启动guns

    eclipse部署guns: 1.import -> 搜索maven -> Existing Maven Projects -> 选择guns根目录 2.修改配置文件: spring ...

  2. ElasticSearch_学习_01_单实例安装与分布式安装

    一.前言 二.下载 1.下载地址 https://www.elastic.co/downloads/past-releases 三.单实例安装 直接解压,window下运行 elasticsearch ...

  3. hdu1085 Holding Bin-Laden Captive!(母函数)

    简单的母函数应用. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstrin ...

  4. linux 缺少libxxx.a 静态链接库

    首先去官方网站下载gdb的源码包,我下载的7.4.1版本的源码包,解压开来,进入到源码包的根目录下.对于一个源码包,拿到手里首先要阅读的就是README,然后看一下INSTALL文件,这个文件里编译源 ...

  5. mysql分表和分区实际应用简介

    一,什么是mysql分表,分区 什么是分表,从表面意思上看呢,就是把一张表分成N多个小表,具体请看mysql分表的3种方法 什么是分区,分区呢就是把一张表的数据分成N多个区块,这些区块可以在同一个磁盘 ...

  6. 我的 Linux 配置

    系统版本 Ubuntu 18.04 一名老年弱智 OI 选手的 Linux 配置 文本编辑器: Sublime Text 中文补丁,关闭自动补全,自动联想,括号匹配,字号 15 编译器: g++ (然 ...

  7. Linux命令学习(21):netstat命令

    版权声明 更新:2017-06-13博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的netstat ...

  8. [独孤九剑]Oracle知识点梳理(七)数据库常用对象之Cursor

    本系列链接导航: [独孤九剑]Oracle知识点梳理(一)表空间.用户 [独孤九剑]Oracle知识点梳理(二)数据库的连接 [独孤九剑]Oracle知识点梳理(三)导入.导出 [独孤九剑]Oracl ...

  9. 3、Selenium调用IEDriverServer打开IE浏览器

    学习Selenium时若想调用IE浏览器,均需要以下步骤 (1).http://selenium-release.storage.googleapis.com/index.html 下载IEDrive ...

  10. java代码swing编程JPaswordField类

    总结:JPasswordField类是JTextField类的子类.用户在JPasswordField对象中输入的字符会被其他的字符替代 而挡住,JPasswordFiled组件主要用来输入口令 pa ...