#define ABS(x) ((x)>=0?(x):(0)))
/**
* This is Function for Hex to Ascii.
* This is a detail description.
* @param[in] hex data, such as 0x789A.
* @param[out] Ascii data, such as '7''8''9''A'.
* @par Created in 2018-08-15
*/
//default 10 is 0x0A, and A is Capital
uint8_t* HextoAscii(uint16_t hex_byte)
{
int i;
uint8_t tmp = 0;
static uint8_t result[4] = {0};
for(i = 0; i < 4; i++){
tmp = hex_byte & 0x000f;
hex_byte = hex_byte >> 4;
if((tmp>=0)&&(tmp<=9))
{
result[3-i] = tmp + 0x30;
}
else if((tmp >= 10)&&(tmp <= 15))
{
result[3-i] = tmp + 0x37;
}
else
{
result[3-i] = 0xff;
}
}
return result;
}
/**
* This is Function for Hex to Ascii.
* This is a detail description.
* @param[in] char, such as uint8_t array[] = { 0x31, 0x32, 0x33, 0x34, 0x35,0x36,0x37,0x38};
nLen, such as sizeof(array)/sizeof(array[0]);
* @param[out] Ascii data, such as "3132333435363738".
* @par Created in 2018-11-13
*/
void HexToStr(char *pbDest, uint8_t *pbSrc, int nLen)
{
char ddl,ddh;
int i;
int p = 0;
for (i=1; i<= nLen; i++)
{
ddh = 48 + pbSrc[i-1] / 16;
ddl = 48 + pbSrc[i-1] % 16;
if (ddh > 57) ddh = ddh + 7;
if (ddl > 57) ddl = ddl + 7;
pbDest[p*2] = ddh;
pbDest[p*2+1] = ddl;
p++;
/* Every 8Byte adds "\r\n"
if(i % 8 == 0){
pbDest[p*2] = '\r';
pbDest[p*2+1] = '\n';
p++;
}
*/
}
}
/**
* This is Function for Ascii to Hex.
* This is a detail description.
* @param[in] Ascii data, such as '3'.
* @param[out] hex data, such as 0x33.
* @par Created in 2018-08-15
*/
unsigned char AsciiToHex(unsigned char bChar)
{
if((bChar>=0x30)&&(bChar<=0x39)){
bChar -= 0x30;
}
else if((bChar>=0x41)&&(bChar<=0x46)) // Capital
{
bChar -= 0x37;
}
else if((bChar>=0x61)&&(bChar<=0x66)) //littlecase
{
bChar -= 0x57;
}
else
{
bChar = 0xff;
}
return bChar;
}
/**
* This is Function for Ascii to Hex.
* This is a detail description.
* @param[in] Ascii data, such as BYTE pbSrc[] = "3132333435363738";
nLen, such as sizeof(array)/sizeof(array[0]);
* @param[out] hex data, such as { 0x31, 0x32, 0x33, 0x34, 0x35,0x36,0x37,0x38};
* @par Created in 2018-11-15
*/
void StrToHex(BYTE *pbDest, BYTE *pbSrc, int nLen)
{
char h1,h2;
BYTE s1,s2;
int i;
for (i=0; i<nLen; i++)
{
h1 = pbSrc[2*i];
h2 = pbSrc[2*i+1];
s1 = toupper(h1) - 0x30;
if (s1 > 9)
s1 -= 7;
s2 = toupper(h2) - 0x30;
if (s2 > 9)
s2 -= 7;
pbDest[i] = s1*16 + s2;
}
}
/**
* This is the Function for Ascii to 4Byte.
* This is a detail description.
* @param[in] Ascii data and the length of data, such as char test[]={'3','4','5','6'} and 4.
* @param[out] hex data, such as 0x5634.
* @par Created in 2018-08-15
modified in 2018-08-15 Change output byte order
*/
uint16_t AsciiToHex_4Byte(uint8_t *bChar, uint8_t bCharLen)
{
int i ;
uint16_t res = 0x0000;
for(i = 0;i < bCharLen; i++){
char tmp = *bChar;
if((tmp>=0x30)&&(tmp<=0x39)){
tmp -= 0x30;
}
else if((tmp>=0x41)&&(tmp<=0x46)) // Capital
{
tmp -= 0x37;
}
else if((tmp>=0x61)&&(tmp<=0x66)) //littlecase
{
tmp -= 0x57;
}
else
{
tmp = 0xff;
}
res = (tmp &0x0f) + (res << 4);
bChar++;
if(!bChar){
return 0;}
}
res = ((res & 0xFF00) >> 8)+ ((res & 0x00FF) <<8);
return res;
}
/**
* This is the Function for changing Char to hex.
* This is a detail description.
* @param[in] char data, such as uint8_t test=0x04.
* @param[out] hex data, such as 0x34.
* @par Created in 2018-10-22
*/
uint8_t CharToHex(uint8_t bHex){
if((bHex >= 0 ) && (bHex <= 9)) {
bHex += 0x30;
}
else if ((bHex >= 10) && (bHex <= 15)){
bHex += 0x37;
}
else{
bHex = 0xFF;
}
return bHex;
}
/**
* @file This function is used for getting the length of a data group.
* @brief Notice that that data group must be initialled zero, and element in the data group can be zero.
* @details This is the detail description.
* @author Huang Zhudong
* @date 2018-08-23
* @version v1.0
* @par Copyright (c):
* @par History:
*/
uint8_t PtrLen(uint8_t *p){
uint8_t res = 0;
while(*p++){
res++;
}
return res;
}
/**
* This is a function which is changed by memcpy.
* The difference between memcpy and FastStract is whether the param[out] is the head of pszDest.
* @param[in] inArgName input argument description.
* @param[out] the head of pszDest .
* @par Created in 2018-08-22
modified in 2018-10-22 Change return type
*/
void *FastStrcat(uint8_t *pszDest, const uint8_t* pszSrc, size_t len)
{
uint8_t *p = pszDest;
if((pszDest == NULL) || (pszSrc == NULL)) return p;
while(pszDest++ == NULL);
pszDest--;
while(len--) {
*pszDest++ = *pszSrc++;
}
return p;
}
/**
* This function is used for reversing escape character.
* As for requriment,
the data 0x04 0x06 should be replaced by 0x02,
the data 0x04 0x07 should be replaced by 0x03,
the data 0x04 0x04 should be replaced by 0x04.
* @param[in] size_t size is the size of src, whose unit is byte. desc_size is the size of desc which is used for get the length of desc.
* @param[out] outArgName output argument description.
* @par for using example:
uint8_t test[] = {0x00,0x01,0x04,0x06,0x04,0x07,0x04,0x04,0x05,0x06,0x07,0x08,0x09,0x010};
int test_len = sizeof(test)/sizeof(test[0]);
uint8_t res1[20] = {0};
int res1_len = 0;
UnEcpChrct(res1,&res1_len,test,test_len);
There is result that res1_len is the length of res1.
* @par Created in 2018-08-22
modify in 2018-08-23 Add a parameter to get the length of desc.
*/
void *UnEcpChrct(void *desc, size_t *desc_size, const void * src, size_t src_size)
{
if((desc == NULL) && (src == NULL))
{
return NULL;
}
uint8_t *desc1 = (uint8_t*)desc;
uint8_t *src1 = (uint8_t*)src;
while(src_size)
{
if(*src1 == 0x04 && *(src1+1) == 0x06){
*desc1 = 0x02;
src1++;
src_size--;
}
else if(*src1 == 0x04 && *(src1+1) == 0x07){
*desc1 = 0x03;
src1++;
src_size--;
}
else if(*src1 == 0x04 && *(src1+1) == 0x04){
src1++;
src_size--;
*desc1 = 0x04;
}
else{
*desc1 = *src1;
}
(*desc_size)++;
desc1++;
src1++;
src_size--;
}
return desc;
}
/**
* This function is used for escape character.
* As for requriment,
the data 0x02 should be replaced by 0x04 0x06,
the data 0x03 should be replaced by 0x04 0x07,
the data 0x04 should be replaced by 0x04 0x04.
* @param[in] size_t size is the size of src, whose unit is byte. desc_size is the size of desc which is used for get the length of desc.
* @param[out] outArgName output argument description.
* @par
* @par Created in 2018-08-23
*/
void *EcpChrct(void *desc, size_t *desc_size, const void * src,size_t src_size){
if((desc == NULL) && (src == NULL))
{
return NULL;
}
uint8_t *desc1 = (uint8_t*)desc;
uint8_t *src1 = (uint8_t*)src;
while(src_size--)
{
if(*src1 == 0x02){
*desc1 = 0x04;
*(++desc1) = 0x06;
(*desc_size)++;
}
else if(*src1 == 0x03){
*desc1 = 0x04;
*(++desc1) = 0x07;
(*desc_size)++;
}
else if(*src1 == 0x04){
*desc1 = 0x04;
*(++desc1) = 0x04;
(*desc_size)++;
}
else{
*desc1 = *src1;
}
(*desc_size)++;
desc1++;
src1++;
}
return desc;
}