AVR Tutorials
The Best AVR Microcontroller Tutorials on the Web

String to Hex C function - StringToHex()

The StringToHex() Function


The following AVR C function StringToHex() was implemented for the AVR microcontroller. It takes a string of characters string and convert it to its hex equivalent which is then stored in hexstring.


StringToHex() Implementation
/*
*  Compiler: AVR GNU C Compiler (GCC)
*  Author: AVR Tutorials
*  Website: www.AVR-Tutorials.com
*/
 
void StringToHex(unsigned char* string, unsigned char* hexstring)
{
       unsigned char ch,i,j,len;
 
       len = strlen(string);
 
       for(i=0,j=0;i<len;i++,j+=2)
       {
            ch = string[i];
 
            if( ch >= 0 && ch <= 0x0F)
            {
                hexstring[j] = 0x30;
 
                if(ch >= 0 && ch <= 9)
                    hexstring[j+1] = 0x30 + ch;
                else
                    hexstring[j+1] = 0x37 + ch;
            }
            else  if( ch >= 0x10 && ch <= 0x1F)
            {
                hexstring[j] = 0x31;
                ch -= 0x10;
 
                if(ch >= 0 && ch <= 9)
                    hexstring[j+1] = 0x30 + ch;
                else
                    hexstring[j+1] = 0x37 + ch;
            }
            else  if( ch >= 0x20 && ch <= 0x2F)
            {
                hexstring[j] = 0x32;
                ch -= 0x20;
 
                if(ch >= 0 && ch <= 9)
                    hexstring[j+1] = 0x30 + ch;
                else
                    hexstring[j+1] = 0x37 + ch;
            }
            else  if( ch >= 0x30 && ch <= 0x3F)
            {
                hexstring[j] = 0x33;
                ch -= 0x30;
 
                if(ch >= 0 && ch <= 9)
                    hexstring[j+1] = 0x30 + ch;
                else
                    hexstring[j+1] = 0x37 + ch;
            }
            else  if( ch >= 0x40 && ch <= 0x4F)
            {
                hexstring[j] = 0x34;
                ch -= 0x40;
 
                if(ch >= 0 && ch <= 9)
                    hexstring[j+1] = 0x30 + ch;
                else
                    hexstring[j+1] = 0x37 + ch;
            }
            else  if( ch >= 0x50 && ch <= 0x5F)
            {
                hexstring[j] = 0x35;
                ch -= 0x50;
 
                if(ch >= 0 && ch <= 9)
                    hexstring[j+1] = 0x30 + ch;
                else
                    hexstring[j+1] = 0x37 + ch;
            }
            else  if( ch >= 0x60 && ch <= 0x6F)
            {
                hexstring[j] = 0x36;
                ch -= 0x60;
 
                if(ch >= 0 && ch <= 9)
                    hexstring[j+1] = 0x30 + ch;
                else
                    hexstring[j+1] = 0x37 + ch;
            } 
            else  if( ch >= 0x70 && ch <= 0x7F)
            {
                hexstring[j] = 0x37;
                ch -= 0x70;
 
                if(ch >= 0 && ch <= 9)
                    hexstring[j+1] = 0x30 + ch;
                else
                    hexstring[j+1] = 0x37 + ch;
            }
       }
       hexstring[j] = 0x00;
}