十六进制字符串与char字节数组的转换函数 II

来源:互联网 发布:淘宝店铺红包 编辑:程序博客网 时间:2024/05/18 02:45

 

//89860099--->0x89,0x86,0x00,0x99int StrToEBCD(char *buf,char *ucBuffer,int BufLen){    unsigned char temp1,temp2;    int Len=BufLen/2,i;    for (i=0; i<Len; i++)    {        temp1=buf[i*2];        if (temp1>='a')            temp1=temp1 - 'a' + 10;        if (temp1>='A')            temp1=temp1 - 'A' + 10;        if (temp1>='0')            temp1=temp1-'0';        temp2=buf[i*2 + 1];        if (temp2>='a')            temp2=temp2 - 'a' + 10;        if (temp2>='A')            temp2=temp2 - 'A' + 10;        if (temp2>='0')            temp2=temp2-'0';        ucBuffer[i]=((temp1&0x0f)<<4)|(temp2&0x0f);    }    return 0;}
//convert EBCD into string//example: 0x79,0x85,0x00,0x99--->79850099unsigned char  istoASCs_lib(unsigned char* desBuffer,BYTE* srcBuffer,int len){ int i;char ch;for(i=0;i<len;i++){ch = srcBuffer[i] >> 4;if((ch >= 0)&&(ch <= 9)){desBuffer[2*i] = ch +'0';}else if((ch >=10)&&(ch <= 15)){desBuffer[2*i] = ch + 55;}elsedesBuffer[2*i] = '*';ch = srcBuffer[i] & 0x0F;if((ch >= 0)&&(ch <= 9)){desBuffer[2*i+1] = ch +'0';}else if((ch >=10)&&(ch <= 15)){desBuffer[2*i+1] = ch + 55;}elsedesBuffer[2*i+1] = '*';}desBuffer[2*i] = '\0';return 0;}



 

原创粉丝点击