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

Convert a char string to hex

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







Convert a char string to hex
PostPosted: Tue Jan 17, 2006 11:40 am     Reply with quote

Hello,

is there any function to convert a char string of 2 characters for example : char test[1]

to an hexadecimal number : byte hex ?

Many Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 17, 2006 12:46 pm     Reply with quote

Here's how to find the answer:
Look at the chart of built-in functions on page 98 (in the Acrobat reader)
of the CCS manual. It has a list of standard string handling or
character handling functions. At the top of the chart, it lists functions
such as atoi, atol, etc. These mean "ASCII to integer", etc.

The source code for these functions is in stdlib.h, which is in this folder:
c:\Program Files\PICC\Drivers

If you think those functions have too much code, there are truncated
versions posted in various places on the forum:
http://www.ccsinfo.com/forum/viewtopic.php?t=17563&highlight=adtol
mdares



Joined: 26 Oct 2005
Posts: 19

View user's profile Send private message

PostPosted: Tue Jan 17, 2006 1:55 pm     Reply with quote

You can just cast a character to it's ascii value using (int), but you didn't look very hard if your asking this question.

Mattd
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 17, 2006 2:24 pm     Reply with quote

If he has an array of two characters, such as "15", the array consists
of two ASCII bytes, 0x31 and 0x35.

I assume he wants to convert that number to an integer: 0x0F

You can't do that by casting.
newguy



Joined: 24 Jun 2004
Posts: 1908

View user's profile Send private message

PostPosted: Tue Jan 17, 2006 2:42 pm     Reply with quote

I do this with something I built. I have to download hex codes to an on-board I2C EEPROM via RS232.

Unless you convert lowercase ascii letters (a-f) to uppercase before you do this conversion, it will fail. I simply make sure the caps lock key is on before I do my downloads.

I use this function with a interrupt-driven serial receive routine, very similar to what has been posted on this board dozens of times.

All the code does is search through the received data buffer and converts the ascii digits 0-9 and a-f into their hex equivalents, 0 - 15. It then does a second pass through the data, taking pairs of characters and combining them into one 8 bit value. You should be able to follow it.

Code:
void decode_data(void) {
   int16 write_address = 0;
   int8 data[40], i;
   int1 internal = FALSE;
   // data string standard format:
   // $ - command start
   // A - address, 4 characters, each representing a hex number
   // if first character of address = 'I', then data must be written to internal
   // EEPROM, starting at address 0
   // D - data, up to 8 characters, each representing a hex number
   // End$ - end string, initiates eeprom save, echoes acknowledgement, restores A/D function
   // do validity check first
   if (rx_buffer[0] != 'A' || (data_length % 2 != 0)) {
      bputc(0x0d); // cr
      bputc(0x0a); // lf
      bputc("Invalid command");
      bputc(0x0d); // cr
      bputc(0x0a); // lf
   }
   else if (rx_buffer[5] == 'D') { // write
      bputc(0x0d); // cr
      bputc(0x0a); // lf
      bputc("Understood - data write");
      bputc(0x0d); // cr
      bputc(0x0a); // lf
      // assemble address
      for (i = 1; i < 5; i++) {
         if (rx_buffer[i] == 'I') { // do internal write
            internal = TRUE;
            i = 5;
         }
         else if (rx_buffer[i] > 0x39) {
            rx_buffer[i] = rx_buffer[i] - 0x37;
         }
         else {
            rx_buffer[i] = rx_buffer[i] - 0x30;
         }
      }
      write_address = rx_buffer[4] + (0x10 * rx_buffer[3]) + (0x100 * rx_buffer[2]) + (0x1000 * rx_buffer[1]);
      printf(bputc,"Address = %lx",write_address);
      bputc(0x0d); // cr
      bputc(0x0a); // lf
      for (i = 6; i < (6 + data_length); i++) {
         if (rx_buffer[i] > 0x39) {
            rx_buffer[i] = rx_buffer[i] - 0x37;
         }
         else {
            rx_buffer[i] = rx_buffer[i] - 0x30;
         }
      }
      data_length = data_length / 2;
      for (i = 0; i < data_length; i++) {
         data[i] = 0x10 * rx_buffer[6 + (2*i)];
         data[i] = data[i] + rx_buffer[7 + (2*i)];
         if (!internal) {
            write_one_byte(write_address + i, data[i]);
         }
         else {
            write_eeprom(i, data[i]);
            delay_ms(15);
         }
      }
   }
   else if (rx_buffer[5] == 'R') { // read from address
      bputc(0x0d); // cr
      bputc(0x0a); // lf
      bputc("Understood - data read");
      bputc(0x0d); // cr
      bputc(0x0a); // lf
      // assemble address
      for (i = 1; i < 5; i++) {
         if (rx_buffer[i] == 'I') { // do internal write
            internal = TRUE;
            i = 5;
         }
         else if (rx_buffer[i] > 0x39) {
            rx_buffer[i] = rx_buffer[i] - 0x37;
         }
         else {
            rx_buffer[i] = rx_buffer[i] - 0x30;
         }
      }
      write_address = rx_buffer[4] + (0x10 * rx_buffer[3]) + (0x100 * rx_buffer[2]) + (0x1000 * rx_buffer[1]);
      printf(bputc,"Address = %lx",write_address);
      bputc(0x0d); // cr
      bputc(0x0a); // lf
      if (internal) {
         for (i = 0; i < 16; i++) {
            bputc(read_eeprom(i));
         }
      }
      else {
         bputc(load_data_from_address(write_address));
         for (i = 0; i < 63; i++) {
            bputc(load_data_no_address());
         }
      }
   }
}
mdares



Joined: 26 Oct 2005
Posts: 19

View user's profile Send private message

PostPosted: Tue Jan 17, 2006 2:54 pm     Reply with quote

I see, I think your right PCM, I was going on he char test[1] as what he wants to 'convert'. eg

for i = 1 to n {
some_int[i] = (int) some_char[i]
}
Kookoo
Guest







PostPosted: Wed Jan 18, 2006 4:00 am     Reply with quote

Hello,

i have to get the date and time for example :

char year[2];
char month[2];
char date[2];

printf("\n\rYear : ");
gets(year);
printf("\n\rMonth : ");
gets(month);
printf("\n\rDate : ");
gets(date);

For example, if i put :

06
01
18

I will have :
year[0] => 0
year[1] => 6
month[0] => 0
month[1] => 1
date[0] = > 1
date[1] = > 8


I have a real time clock that i want to initialize with this function :

RTC_set_datetime(date, month, year, hour, min, sec);

But this function take bytes in parameters, and i have char strings.
It should be like this :

RTC_set_datetime(18, 01, 06, 10, 58, 00);

for 2006/01/18 10:58:00

How can i do this ?

Thanks !
Kookoo
Guest







PostPosted: Wed Jan 18, 2006 6:36 am     Reply with quote

I used atoi function with something pretty much like this :

year= atoi(year_char, 16);

It works very well.
Thanks a lot.
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