Getting Decimal Value instead of Hex

Discussion in 'Embedded Systems and Microcontrollers' started by bhargav shankhalpara, Sep 11, 2014.

  1. bhargav shankhalpara

    Thread Starter Member

    May 9, 2010
    18
    0
    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.
     
  2. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    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);
     
  3. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    TOTAL = ((S1*10) + S2)*10 + S3;
     
  4. sirch2

    Well-Known Member

    Jan 21, 2013
    1,008
    351
    As I read it S1, S2 and S3 are chars
     
Loading...