View previous topic :: View next topic |
Author |
Message |
rhinojo7 Guest
|
Character String to Hex |
Posted: Thu Jul 27, 2006 3:06 pm |
|
|
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
|
|
Posted: Thu Jul 27, 2006 3:18 pm |
|
|
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
|
This may help |
Posted: Thu Jul 27, 2006 6:50 pm |
|
|
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
|
The improved version. |
Posted: Sun Jul 30, 2006 6:36 pm |
|
|
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
|
|
Posted: Tue Sep 02, 2014 3:16 am |
|
|
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);
} |
|
|
|
|