Skip to content

Instantly share code, notes, and snippets.

@imam-san
Created February 18, 2020 17:08
Show Gist options
  • Save imam-san/7ad82c53c71f52ca6d97211f8914c703 to your computer and use it in GitHub Desktop.
Save imam-san/7ad82c53c71f52ca6d97211f8914c703 to your computer and use it in GitHub Desktop.
BCD Converter.c
int Hexascii(unsigned char *dest, unsigned char *src, int size)
{
int i;
if (size > 0)
{
for (i = 0; i < (size / 2); i++)
{
*(dest + (2 *i)) = ((*(src + i) &0xF0) >> 4) + 0x30;
if (*(dest + (2 *i)) > 0x39)
*(dest + (2 *i)) += 0x07;
*(dest + (2 *i) + 1) = (*(src + i) &0x0F) + 0x30;
if (*(dest + (2 *i) + 1) > 0x39)
*(dest + (2 *i) + 1) += 0x07;
}
if (size % 2 != 0)
{
/* traitement size impaire */
*(dest + (2 *i)) = ((*(src + i) &0xF0) >> 4) + 0x30;
if (*(dest + (2 *i)) > 0x39)
*(dest + (2 *i)) += 0x07;
}
return (0);
}
else
return (1);
}
unsigned long ulHextoLongLittle(unsigned long l, unsigned char *buff)
{
unsigned char buffA34 [5];
memset(buffA34,0x00,sizeof buffA34);
buffA34[0]=(int)((l & 0xFF000000)>>24);
buffA34[1]=(int)((l & 0x00FF0000)>>16);
buffA34[2]=(int)((l & 0x0000FF00)>>8);
buffA34[3]=(int)((l & 0x000000FF));
memcpy(buff, buffA34,4);
return l;
}
unsigned long ulHextoLongBig(unsigned long l, unsigned char *buff)
{
unsigned char buffA34 [5];
memset(buffA34,0x00,sizeof buffA34);
buffA34[3]=(int)((l & 0xFF000000)>>24);
buffA34[2]=(int)((l & 0x00FF0000)>>16);
buffA34[1]=(int)((l & 0x0000FF00)>>8);
buffA34[0]=(int)((l & 0x000000FF));
memcpy(buff, buffA34,4);
return l;
}
unsigned long ucLittletoLong(unsigned char * input)// only support 4
{
unsigned char buff[5];
memset(buff, 0x00, sizeof buff);
buff[0]=input[3];
buff[1]=input[2];
buff[2]=input[1];
buff[3]=input[0];
return HextoLong(buff, 4);
}
unsigned long HextoLong( unsigned char * input,int len)
{
unsigned long retAmt=0;
char temp[100];
int i,m=1;
i=len*2;
memset(temp,0x00,sizeof temp);
Hexascii(temp,input, i);
i--;
while (i>=0)
{
if (temp[i] >= '0' && temp[i] <= '9')
retAmt=retAmt+((int)temp[i]-48)*m;
if ((temp[i] >= 'A' && temp[i] <= 'F')||(temp[i] >= 'a' && temp[i] <= 'f'))
retAmt=retAmt+((int)temp[i]-55)*m;
m=m*16;
i--;
}
return retAmt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment