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

Array ascii to array hex

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



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

Array ascii to array hex
PostPosted: Tue Mar 18, 2014 1:51 pm     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Tue Mar 18, 2014 2:59 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Mar 18, 2014 4:17 pm     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Tue Mar 18, 2014 4:34 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Mar 18, 2014 4:53 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Mar 18, 2014 4:59 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Mar 18, 2014 5:18 pm     Reply with quote

Thank you...
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