CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Character String to Hex

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rhinojo7
Guest







Character String to Hex
PostPosted: Thu Jul 27, 2006 3:06 pm     Reply with quote

Hello all, what I'm trying do is read in a character string from the PC through the serial port. The character coming in form the PC is supposed to be Hex Data. My problem is how do I convert the string of data into hex.

here is an example of what I'm trying to do:
Lets say this is the string coming in from the PC:
1A2B3C4D5E

What I want to do is make that string into the following (I'm using int8 as the type for the data: int8 data[5];)

data[0]=0x1A
data[1]=0x2B
data[2]=0x3C
data[3]=0x4D
data[4]=0x5E

Does anybody have any idea of how to do that. I would really appreciate the help.

Thanks

rhinojo
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jul 27, 2006 3:18 pm     Reply with quote

CCS has a function called atoi_b16() that will do it. It's in the Loader.c
file. Look in this folder: c:\program files\picc\drivers
kel



Joined: 17 Oct 2005
Posts: 68
Location: Brisbane

View user's profile Send private message

This may help
PostPosted: Thu Jul 27, 2006 6:50 pm     Reply with quote

The following function works fine but it has got its side effect.
Code:
void String2Hex(char *arr, byte StartPos, byte EndPos){
             byte counter;
             byte cnt;
             char buff[5]="";/*clear the buffer.you are interested in 0xXX*/
                for(counter=StartPos; counter < EndPos; counter++)
                       buff[counter]=arr[counter];
     
                   for(cnt=StartPos; cnt < EndPos; cnt++)
                       printf("0X%c",buff[cnt]);

}
 /*Test program*/
   void main(void)
{
               char xc[]="1A2B3C4D5E";
               String2Hex(xc, 0, 2)   /*prints 0X1A */
                String2Hex(xc, 1, 3)  /*prints 0x2B */
 }
Hope this helps. PCM programmer please improve this function should you find any flause.I have not tested this function under the CCS enviroment but have tested it under GCC c compiler and it works fine.
cheers
kel



Joined: 17 Oct 2005
Posts: 68
Location: Brisbane

View user's profile Send private message

The improved version.
PostPosted: Sun Jul 30, 2006 6:36 pm     Reply with quote

This function checks for the difference of start and end position. if it's more than two, it displays error message. This way the mn cannot be more than 2 especially if data is required as follows:
data[0]=0x1A
data[1]=0x2B
data[2]=0x3C
data[3]=0x4D
data[4]=0x5E



Code:

void String2Hex(char *arr, byte StartPos, byte EndPos){
             byte counter;
             byte cnt;
            byte mn=Endpos-Startpos;
// to restrict the function to print only 2 hex numbers, the mn must be //equal to 2(mn=2)
          if(mn>1 && mn <3) {
               char buff[5]="";/*clear the buffer.you are interested in 0xXX*/
                for(counter=StartPos; counter < EndPos; counter++)
                       buff[counter]=arr[counter];

                    Sartpos=0;
                    Endpos=2; /*print 2 hex only*/
                   for(cnt=StartPos; cnt < EndPos; cnt++)
                       printf("0X%c",buff[cnt]);
               } else  printf("Error mn bust be 2 !!");

}
 /*Test program*/
   void main(void)
{
               char xc[]="1A2B3C4D5E";
               String2Hex(xc, 0, 2)   /*prints 0X1A */
                String2Hex(xc, 1, 3)  /*prints 0x2B */
 }
thientaisodo



Joined: 31 Aug 2009
Posts: 5

View user's profile Send private message

PostPosted: Tue Sep 02, 2014 3:16 am     Reply with quote

This may be help for anyone.
Function convert string (8bit) to hex (8bit)

Code:

char   demo_string[2]="A0";
int8    demo_hex;

demo_hex=atoh(demo_string);
// demo_hex = 0xA0

signed int atoh(char *s)
{
   unsigned int temp[2], count, result;
   char c;
   
   result = 0;
   count = 0;
   
   for(count=0;count<2;count++)
   {
      c = s[count];
      if (c >= '0' && c <= '9')
      {
         temp[count] = c - '0';
      }
      else  if (c >= 'A' && c <= 'F')
      {
         temp[count] = c - '7';
      }
      else  if (c >= 'a' && c <= 'f')
      {
         temp[count] = c - 'W';
      }
      else
      {
         temp[count] = 0;
      }
   }
   result = (((temp[0] << 4) & 0xf0)|(temp[1] & 0x0f));

   return(result);
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group