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.

3 byte Hex to decmial in c

Status
Not open for further replies.

ravi_p

Member level 1
Joined
Jun 29, 2004
Messages
38
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
337
Hi

I am doing a project i am looking for routine in c to convert 3 byte hex value to decmial. i am using keil

Thanks
 

Please explain more clearly what you mean by "hex" and "decimal". Do you want to convert an ASCII string such as "12FC3B" into another ASCII string "1244219"? Or something else?
 

echo47 said:
Please explain more clearly what you mean by "hex" and "decimal". Do you want to convert an ASCII string such as "12FC3B" into another ASCII string "1244219"? Or something else?

byte1=0x02; byte2=0x77; byte3=0x2F; (02772F) to be converted in decmail(2585343)

I am looking for a routine in c.

Thanks
Ravi
 

data union {
unsigned long dec;
unsigned char hex[4];
}Convert;

Convert.hex[0]=0x00;Convert.hex[1]=0x27;
Convert.hex[2]=0x72;Convert.hex[3]=0xff;

//Convert.dec == 2585343
 

Here is a solution assuming you have 32 bit integers.
The result you want is called BCD (Binary Coded Decimal) meaning each decimal digit is represented by a binary number, in this case a binary integer.

Code:
int val = b2 * 65536 + b1 * 256 + b0;
const int NUM_BCD = 8; // ffffff = 16,777,215 : 8 bcd digits max
int[] aBCD = new int[NUM_BCD]; // define an array to hold the BCD digits
int div = 10000000; 
for(int n=NUM_BCD-1; n>=0; n--)
{
     aBCD[n] = val/div; // get the integer
     val = val % div;   // use remainder for next calc
     div = div/10; 
}
The aBCD array will hold the BCD digits; the 0th element will hold the MSD.

If you only have 16 integers, you will have to do it in 2 steps.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top