Hi, im lookin for a c function which will take hex values stored in two different integer variables say TH1,TL1 . So say TH1 has FF and TL1 has 00,then i need a program that will convert the effective value FF00 to decimal ,ie 65280 stored in a variable say Dec.

Recommended Answers

All 2 Replies

any int variable is inherently an *integer* the value is not stored in any particular base .... hex or decimal or binary, it doesnt matter. the only difference is when you print it. printf("hex value %04X = decimal value %d\n",Dec, Dec); but also, what youre asking to do is to concatenate an upper byte to a lower byte. or you could also say you're adding a left-shifted value to another value.

you could do it like this: Dec = (TH1 << 8) + TL1; .

commented: Yep, an integer is an integer :) +4

you also can convert HEX number to string then convert them to decimal or integer again.
I mean

sprintf( tmp, "%02X%02X", TH1, TL1 );
sscanf( tmp, "%X", &intHex );

this is a second way to do this but jephthah's post is better ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.