lcd/USER/ctranscode.c
2026-05-22 17:19:03 +08:00

131 lines
2.4 KiB
C

/***
* @文件名称: ctranscode.c
* @系统名称: 公共库
* @模块名称: 底层函数库
* @功能说明: 编码转换函数
* @当前版本: 1.0.0
* @修改说明: 创建文件
*/
#include "public.h"
#include "define.h"
//static BYTE szHexByte[] =
//{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','A', 'B', 'C', 'D', 'E', 'F'};
int Table_ASC_HEX[]={
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
0x40,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f
};
const char HexToAsc1[]="0123456789ABCDEF";
void Byte2String(int len, unsigned char *InBuf, unsigned char *OutBuf)
{
for(; len>0; len--)
{
(*OutBuf++) = HexToAsc1[((*InBuf)>>4)&0xf];
(*OutBuf++) = HexToAsc1[(*InBuf++)&0xf];
}
}
void String2Byte(int len, unsigned char *InBuf, unsigned char *OutBuf)
{
for(; len>0; len-=2, InBuf+=2)
{
(*OutBuf++) = (Table_ASC_HEX[(*InBuf)]<<4)|Table_ASC_HEX[*(InBuf+1)];
}
}
void hextoasc(unsigned char *asc,unsigned char *hex,int len)
{
int i;
int ch;
for(i=0;i<len;i++)
{
ch=hex[i]>>4;
if(ch>=0&&ch<=9)
{
asc[i*2]=ch+'0';
}
if(ch>=0xA&&ch<=0xF)
{
asc[i*2]=ch-0xa+'A';
}
ch=hex[i]&0x0F;
if(ch>=0&&ch<=9)
{
asc[i*2+1]=ch+'0';
}
if(ch>=0xA&&ch<=0xF)
{
asc[i*2+1]=ch-0xa+'A';
}
}
}
void asctohex(unsigned char *hex,unsigned char *asc,int len)
{
int i;
unsigned char ch;
for(i=0;i<len;i++)
{
ch=asc[i];
if(ch>='0'&&ch<='9')
{
ch-='0';
}
if(ch>='a'&&ch<='f')
{
ch=ch-'a';
ch=ch+10;
}
if(ch>='A'&&ch<='F')
{
ch=ch-'A';
ch=ch+10;
}
if(i%2==0)
{
hex[i/2]=ch<<4;
}else
{
hex[i/2]=hex[i/2]|ch;
}
}
}
void asctoebcd(unsigned char *ebcd,unsigned char *asc,int len)
{
int i;
for(i=0;i<len;i++)
{
if((asc[i]>='0') && (asc[i]<='9'))
ebcd[i]=asc[i]-'0' + 0xF0;
if((asc[i]>='A') && (asc[i]<='F'))
ebcd[i]=asc[i]-'A' + 0xC1;
if((asc[i]>='a') && (asc[i]<='f'))
ebcd[i]=asc[i]-'a' + 0xC1;
}
}