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 support@ccsinfo.com

Get_String of Hex...

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



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

Get_String of Hex...
PostPosted: Fri Jun 20, 2008 7:18 pm     Reply with quote

Dear CCS Members,

I would like to capture a string of hex then store it into the EEPROM...

gethex only support 2 hex digit...

Example:
I need to enter manually via RS232 a string of 48bit in hex this mean
6 bytes in total or 12 hex digits...

FF00FF00FF00 ... it would be too coumbersome to enter hex by hex with gethex so i think get_string fit more in this case... but how to convert the string value into hex after that ?

Any clue?


Many thanks!

Regards,
Laurent
ELCouz



Joined: 18 Jul 2007
Posts: 427
Location: Montreal,Quebec

View user's profile Send private message

PostPosted: Fri Jun 20, 2008 9:17 pm     Reply with quote

A little update:

I've written a partially functioning converter ... please help :-) !

I'm using the code from ckielstra to convert string hex to int8 (http://www.ccsinfo.com/forum/viewtopic.php?p=70543#70543)

Code:

printf("######################\r\n");
printf("#     Add a User     #\r\n");
printf("######################\r\n");
printf("# Type the 48bit ID  #\r\n");
printf("# from the iButton.  #\r\n");
printf("# (12 hex numbers)   #\r\n");
printf("# Example:           #\r\n");
printf("#    FF00FF00FF00    #\r\n");
printf("######################\r\n");
get_string(rhex,BUF_SIZE2-1);
printf("\r\n");
if (strlen(rhex)==12){ //check if the string is 48bit long
  for (i=0;i<7;i++){ //Convert string into int8
  shex[0]=rhex[i];
  shex[1]=rhex[i+1];
  rint=atoi8(shex);
  printf("%X",rint);
  }
}




However , when i chop the string into a string of 2 hex char ... because of this :

Quote:

// Note that this function requires the string to have a length
// of two characters. A zero termination of the string is
// optional.


it fails to show the same hex string at the end thus conversion failed!

if i enter 0000108a05fb

Then the result is 0000000110088A !!! this not the same thing :(

Any help please Smile ?

Have a nice day!
Laurent
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jun 20, 2008 9:24 pm     Reply with quote

Quote:
I would like to capture a string of hex then store it into the EEPROM...

I assume you mean ASCII hex characters. The following routine
converts up to 2 digits of ASCII hex chars to an 8-bit unsigned int.

You can use strlen() to get the length of the string in your buffer.
Then use a for() loop to convert two digits per pass.
Code:

// Convert a string of 1 or 2 ascii hex characters to a binary value.

int8 axtoi(char *ptr)
{
int8 i, temp, value;

value = 0;

for(i = 0; i < 2; i++)       // Convert a maximum of 2 digits
   {
    temp = toupper(*ptr++); // Get the char. Convert to upper case
    if(isxdigit(temp) == FALSE)  // Is ascii char a hex digit ?
       break;          // Break if not
    temp -= 0x30;      // Convert the char to binary
    if(temp > 9)
       temp -= 7;
    value <<= 4;       // shift existing value left 1 nybble
    value |= temp;     // Then combine it with new nybble
   }

return(value);
}


Example:
Code:
void main()
{
char array[20] = {"0123456789ABCDEF"};
int8 len;
char *ptr;
int8 i;

len = strlen(array);
ptr = array;

for(i = 0; i < (len + 1)/2; i++)
   {
    printf("%x \n\r", axtoi(ptr));
    ptr += 2;
   }

while(1);     
}
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