View previous topic :: View next topic |
Author |
Message |
ELCouz
Joined: 18 Jul 2007 Posts: 427 Location: Montreal,Quebec
|
Get_String of Hex... |
Posted: Fri Jun 20, 2008 7:18 pm |
|
|
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
|
|
Posted: Fri Jun 20, 2008 9:17 pm |
|
|
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 ?
Have a nice day!
Laurent |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jun 20, 2008 9:24 pm |
|
|
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);
} |
|
|
|
|