Getting Decimal Value instead of Hex

Thread Starter

bhargav shankhalpara

Joined May 9, 2010
20
Hi all...

i am programming ATMEGA32 microcontroller and ATMEL STUDIO 6 for programming in my application.

in my application user enter 3 difference value which i have store in char variable.

At the end i combine this 3 value in one variable and compare it with predefine value. when i combine in one variable it automatically convert into decimal.

for example i have 3 variable named S1,S2,S3

user enter S1=2,S2=0,S3=9;

at end combine value in TOTAL variable which is INT.

TOTAL = (S3 | (S2 << 4) | (S1 << 8));

so TOTAL value should be 209

but it shows after decimal conversation 521.

when i compare it with 209 this gives no result, but it gives result when my predefine value 521.

i want to get result when user enter 209 and my predefine value also 209.
 

sirch2

Joined Jan 21, 2013
1,037
I'm not sure bit-wise operations are the best approach in this case and anyway you only left-shift your second digit by 4. You should look at the strtol function. An alternative approach is to subtract 48 from the ascii value of the character, something like

TOTAL = (s1-48)*100 + (s2-48)*10 +(s3-48);
 
Top