LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert hex to Decimal and keep leading zeros..

Solved!
Go to solution

Hi,

 

I hope you can help?

 

How to convert hex to Decimal and keep leading zeros

 

I read 002C, hex, and I want to convert it to 0044 decimal.

 

sscanf (MyNum, "%4x", &DecNum); will only give me 44.

 

It have been working up till I started to get leading zeros.

 

We will always have a 4 digit hex input in a range

 

We must have the leading 00 in this case.

 

How is this best done?

 

Thanks for the help

Simon

 

0 Kudos
Message 1 of 3
(4,099 Views)
Solution
Accepted by topic author Srm27

Hi,

 

I don't really understand your problem. Is this stuff what you need ?

int main (int argc, char *argv[])
{
	const char MyNum[] = "002C";
	int DecNum;
	
	sscanf (MyNum, "%4x", &DecNum);
	printf ("%04d", DecNum);
	getchar ();

	return 0;
}

 

"0044" appears on standard output when printf function executes...

Message 2 of 3
(4,092 Views)
Hi, Just what I was looking for. I like when it required a simple answer Thanks Simon
0 Kudos
Message 3 of 3
(4,088 Views)