Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] To convert Hex string to interger using Keil C

Status
Not open for further replies.

RyanHan

Junior Member level 3
Joined
Apr 24, 2012
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,514
Dear all,

I am trying to find a better way to convert a string of 4 Hex character to integer so that I can do the compare and do necessary action.

I am using a wireless communication to get ADC value from sensors and the return was string character of the value in Hex.

e.g. from 0000 to 2EEE.

I saw the atoi function in Keil C but it can only convert 0-9 and not A to F. Is there any function that can convert this 2 bytes data into integer?

Currently my method of conversion was quite lengthy. I will use a lookup table and convert all the string Hex into binary
e.g. 'F' (0x46) --> 0x0F, merge the individual 4 bit into a byte, then merge the 2 byte into an integer. Then only I can process the integer.

Anyone had a better idea? Pls share. Thanks a lot
 

Both KEIL C51 and MDK-ARM offer Standard C libraries, why not use sscanf()?

Code:
#include <stdio.h>

int ahtoi( const char* hxstr )
{
    int number;
    sscanf( hxstr, "%x", &number );
    return number;
}


BigDog
 

Hi bigdog, can I ask about the function u write, I will received the Hex string in a data buffer, let's say the 4 hex to convert into int is in data[x-4], data[x-3], data[x-2] and data[x-1]. How can I write my function in the main so that I can get the return integer?

Is it like this
Code:
unsigned int ADCvalue;

ADCvalue = ahtoi(data[x-4], data[x-3], data[x-2] and data[x-1]);
 

Hi to all,

The very good solution proposed by BigDog requires a null terminated string. So just add a 0x00 after the last hex digit and call the function with the pointer to the first hex digit.

If however the sscanf() adds a long code, you can try using a 'homemade' conversion look like:
Code:
unsigned short hex_to_short(unsigned char* buffer)
{
   unsigned char k,i;
   unsigned short  n; 
   
   for(i=0;i<4;i++)
   {
      k=*(buffer+i)-48;
      if (k>9)           //hex digit not a number 
          k-=7;
      k&=0x0f;       //lowercase or wrong hex digit
      n=(n<<4) | k;
   }
   return n;
}
 
Hi Bigdog,

I try using data buffer to individually called your function. But it still give me illegal pointer conversion. Sorry, really not so good at pointer. Can advise?

Code:
ADCvalue = 0;
for (i=0; i<4; i++)
	{
		ADCvalue = ADCvalue + ahtoi(Data[i]);
	}

- - - Updated - - -

Hi Alex_r,

Great thanks to you. You code is simple to understand for novice like me. I had try it and it works for my application and I understand the concept behind your program. This is my test program and I can convert the string "2EEE" into unsigned int 2EEE. Thanks again

Code:
unsigned int Stringhex_to_hex(unsigned char* buffer)
{
   unsigned char k,i;
   unsigned int  n; 
   
   for(i=0;i<4;i++)
   {
      k=*(buffer+i)-48;
      if (k>9)           //hex digit not a number 
          k-=7;
      k&=0x0f;       //lowercase or wrong hex digit
      n=(n<<4) | k;
   }
   return n;
}

void main (void)
{
 	unsigned int ADCvalue;
	unsigned char Data[4];

	Data[0] = '2';
   	Data[1] = 'E';
	Data[2] = 'E';
	Data[3] = 'E';

	ADCvalue = Stringhex_to_hex(&Data[0]);
	
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top