helloyifa 31f179cb76 init
2025-05-15 14:19:56 +08:00

193 lines
4.1 KiB
C
Executable File

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "common.h"
/*************************************************
Function: string2hex
Description: 字符串转换成hex,要求str只能是大写字母ABCDEF和数字
Input: str:要转换的字符串
Output: hex:转换后的hex字符数组
Return: 0 成功
1 不符合规则,失败
*************************************************/
int string2hex(char* str,char* hex)
{
int i = 0;
int j = 0;
unsigned char temp = 0;
int str_len = 0;
char str_cpy[100] = {'0'};
strcpy(str_cpy,str);
str_len = strlen(str_cpy);
if(str_len==0)
{
return 1;
}
while(i < str_len)
{
if(str_cpy[i]>='0' && str_cpy[i]<='F')
{
if((str_cpy[i]>='0' && str_cpy[i]<='9'))
{
temp = (str_cpy[i] & 0x0f)<<4;
}
else if(str_cpy[i]>='A' && str_cpy[i]<='F')
{
temp = ((str_cpy[i] + 0x09) & 0x0f)<<4;
}
else
{
return 1;
}
}
else
{
return 1;
}
i++;
if(str_cpy[i]>='0' && str_cpy[i]<='F')
{
if(str_cpy[i]>='0' && str_cpy[i]<='9')
{
temp |= (str_cpy[i] & 0x0f);
}
else if(str_cpy[i]>='A' && str_cpy[i]<='F')
{
temp |= ((str_cpy[i] + 0x09) & 0x0f);
}
else
{
return 1;
}
}
else
{
return 1;
}
i++;
hex[j] = temp;
//printf("%02x",temp);
j++;
}
//printf("\n");
return 0 ;
}
int arrayToStr(unsigned char *buf, unsigned int buflen, char *out)
{
char strBuf[33] = {0};
char pbuf[32];
int i;
for(i = 0; i < buflen; i++)
{
sprintf(pbuf, "%02X", buf[i]);
strncat(strBuf, pbuf, 2);
}
strncpy(out, strBuf, buflen * 2);
printf("out = %s\n", out);
return buflen * 2;
}
int StringToHex(char *str, unsigned char *out, unsigned int *outlen)
{
char *p = str;
char high = 0, low = 0;
int tmplen = strlen(p), cnt = 0;
tmplen = strlen(p);
while(cnt < (tmplen / 2))
{
high = ((*p > '9') && ((*p <= 'F') || (*p <= 'f'))) ? *p - 48 - 7 : *p - 48;
low = (*(++ p) > '9' && ((*p <= 'F') || (*p <= 'f'))) ? *(p) - 48 - 7 : *(p) - 48;
out[cnt] = ((high & 0x0f) << 4 | (low & 0x0f));
p ++;
cnt ++;
}
if(tmplen % 2 != 0) out[cnt] = ((*p > '9') && ((*p <= 'F') || (*p <= 'f'))) ? *p - 48 - 7 : *p - 48;
if(outlen != NULL) *outlen = tmplen / 2 + tmplen % 2;
return tmplen / 2 + tmplen % 2;
}
void hex_to_asciistring(unsigned char* str,int size,unsigned char* str1)
{
unsigned char deposit [2];
int i=0;
unsigned char j = 0;
for(i=0;i<size;i++){
deposit[1] = str[i] & 0x0F;
deposit[0] = (str[i] &0xF0) >> 4;
for(j = 0; j < 2; j++){
switch(deposit[j]){
case 0x00:
str1[i*2+j]='0';
break;
case 0x01:
str1[i*2+j]='1';
break;
case 0x02:
str1[i*2+j]='2';
break;
case 0x03:
str1[i*2+j]='3';
break;
case 0x04:
str1[i*2+j]='4';
break;
case 0x05:
str1[i*2+j]='5';
break;
case 0x06:
str1[i*2+j]='6';
break;
case 0x07:
str1[i*2+j]='7';
break;
case 0x08:
str1[i*2+j]='8';
break;
case 0x09:
str1[i*2+j]='9';
break;
case 0x0A:
str1[i*2+j]='A';
break;
case 0x0B:
str1[i*2+j]='B';
break;
case 0x0C:
str1[i*2+j]='C';
break;
case 0x0D:
str1[i*2+j]='D';
break;
case 0x0E:
str1[i*2+j]='E';
break;
case 0x0F:
str1[i*2+j]='F';
break;
default:
return ;
}
}
}
return ;
}
int checksum(unsigned char *data, int length) {
int sum = 0;
for (size_t i = 0; i < length; i++) {
sum += data[i];
}
return sum;
}