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 support@ccsinfo.com

reverse sprintf!

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



Joined: 30 Oct 2003
Posts: 209
Location: Norfolk, England

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

reverse sprintf!
PostPosted: Wed Oct 11, 2006 4:02 am     Reply with quote

Hi,
I thought I would cheat and ask here before I go off and code something. I remember seeing some code posted a few years ago which did this but can't find it in a search. Anyway.

I have two bytes which represent a hex "word" in ASCII. i.e. type bytes 'F' 'E' and I want to convert this to a int8 variable set to 254.

I am sure there is a easy way to do this?

Keep well,

Will
Ttelmah
Guest







PostPosted: Wed Oct 11, 2006 5:02 am     Reply with quote

There have been some published partial 'sscanf' functions, but for a single simple conversion like this, DIY, will be a lot smaller and faster.
The conversion is very simple. If the characters are in an array then:
Code:

#define valhex(x) (x<':')?(x-'0'):(x-'7')

int8 hex_to_bin(char * digits) {
   return (valhex(digits[0])<<4)+(valhex(digits[1]);
}

Call this with the address of the first hex digit.
This will only handle upper case hex. A more generic form, would test/handle the lower case version as well.
There may have been a published version of 'HexToBin' (which is the standard C version of this function), but I can't spot one at present.

Best Wishes
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Oct 11, 2006 8:14 am     Reply with quote

Here is my implementation of atoi(). A bit more difficult to
understand because highly optimized for compact code size.
This implementation is case insensitive.

Code:
#include <18F458.H>
#fuses HS, NOWDT, PUT, NOLVP
#use delay(clock=16000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)


//--------------------------------------------------------
// Convert a hexadecimal string to an unsigned 8 bit integer.
//
// Note that this function requires the string to have a length
// of two characters. A zero termination of the string is
// optional.
// Both lowercase and uppercase characters are accepted.
//--------------------------------------------------------
int8 atoi8(char *string)
{
  int8 result=0;
  int8 i=2;                   
  int8 digit;

  do
  {
    result <<= 4;
    digit = (*string);
    result += (digit & 0x0F);
    if (digit >= 'A')
      result += 9;
    string++;
  } while (--i);

  return result;
}


//==============================
void main(void)
{
  char buffer[3] = {"0A"};
  int8 result;

  result = atoi8(buffer);
  printf("result = %u \n\r", result);

  while(1);
}


Edit 11-Oct-2006: Fixed two bugs in the conversion and added example code. Thanks PCM.
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