View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
Array ascii to array hex |
Posted: Tue Mar 18, 2014 1:51 pm |
|
|
Hi I want to convert a array ascii to array hex.
Example: Code: | char RXC[]={"C8373B0C"}; |
to
Code: | int RXI[]= {0xC8, 0x37, 0x3B, 0x0C}; |
How I can do it? |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Mar 18, 2014 2:59 pm |
|
|
had you thought of parsing the values , two bytes at a time -with
perhaps a strcopy and then atoi()??
how have you tried to do this so far ??
i can think of several ways - but what did you think of ?? |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Tue Mar 18, 2014 4:17 pm |
|
|
Hi Asmboy,
I am try with this code ...
Code: | #include <18F4520.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20000000)
#use rs232(baud=19600, xmit=PIN_C6, rcv=PIN_C7, ERRORS, timeout=5000)// RS232 Estándar
#include <stdlib.h>
int Msg[]={"C8373B0C"};
char Buffer[10];
int Dato[5];
int8 i,j,x;
void MsgRX(){
x = strlen(Msg)/2;
for (i=0; i<x; i++){
for (j=0; j<2; j++){
Buffer[j] = Msg[j+2*i];
}
Dato[i] = atoi(Buffer);
}
}
void main(){
MsgRX();
delay_ms(100);
while(TRUE);
} |
Only work to decimal string, for hex string don't work |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Mar 18, 2014 4:34 pm |
|
|
the value you obtain is actually a one byte binary value,
how you choose to EXPRESS it is another matter....
no register content is inherently hex-decimal-octal or whatever.
in the PIC it is bits in registers.
how you represent it is does not alter the fundamental value of the number
CCS has the tools for that .... |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Tue Mar 18, 2014 4:53 pm |
|
|
Quote: | CCS has the tools for that .... |
Please can you suggest me one of those tools... |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Mar 18, 2014 4:59 pm |
|
|
atoi() tries to determine the input value by searching for a prefix. When the value starts with '0x' it assumes a hexadecimal value, otherwise a decimal value (some variants also can read octal and binary values when starting with a 0 for octal and 0b for binary).
So, for you this can only be used when you add a '0x' on every iteration.
strtol() is a better suited function where you can specify the radix and no prefix is needed.
Same problem as with atoi() though is that it expects a string as input, i.e. an array terminated by a zero. This is not what you have in your code.
It is easy to convert 2 ASCII bytes to binary and you could write the function yourself in a few minutes. In bootloaders the same function can also be found, see the CCS file loader.c where it is called atoi_b16().
Long time ago I wrote an optimized version that I'm willing to share here: Code: | //-----------------------------------------------------------------------------
// Convert a 2 character hexadecimal string to an unsigned 8 bit integer.
//
// Requires only 68 bytes on a PIC18 compared to 500+ bytes for the full blown
// atoi function supplied by CCS.
// This function only looks at the first two characters of the string so it is
// not a requirement for the string to be zero terminated. Both lower case and
// upper case characters are accepted.
//
// Based on code posted on the CCS forum. Original function name was atoi_b16()
// as in the CCS supplied driver file loader.c but implementation is different.
// Posted by Sham, original author unknown.
// https://www.ccsinfo.com/forum/viewtopic.php?p=62934
//-----------------------------------------------------------------------------
int8 atoi8(char *string)
{
int8 MSN; // Most Significant Nibble of the result
int8 LSN; // Least Significant Nibble of the result
MSN = (string[0] - '0'); // Assume numeric and subtract text 0 which is ASCII 30
if (MSN > 9) // The character is actually alphabetic,...
MSN = MSN - 7; // ...adjust for the 7 character gap in the ASCII table
LSN = (string[1] - '0'); // Assume numeric and subtract text 0 which is ASCII 30
if (LSN > 9) // The character is actually alphabetic,...
LSN = LSN - 7; // ...adjust for the 7 character gap in the ASCII table
// Shift the MSN into position (zero fill), strip lower case bit and...
return ((MSN << 4) | (LSN & 0x0F)); // ...merge it with the LSN after the lower case bit is stripped
} |
|
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Tue Mar 18, 2014 5:18 pm |
|
|
Thank you... |
|
|
|