Connect with us

Getting Decimal value instead of hex

Discussion in 'Microcontrollers, Programming and IoT' started by BHARGAV SHANKHALPARA, Sep 11, 2014.

Scroll to continue with content
  1. BHARGAV SHANKHALPARA

    BHARGAV SHANKHALPARA

    35
    0
    Nov 6, 2013
    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. Harald Kapp

    Harald Kapp Moderator Moderator

    10,579
    2,359
    Nov 17, 2011
    S1=2, S1 << 8 = S1*256 = 2*256 = 512
    S2=0, S2 << 4 = S2* 16 = 0*16 = 0
    S3=9

    -> (S3 | (S2 << 4) | (S1 << 8)) = 521, not 209.

    decimal 521 = 0x209 (hex).


    The issue is with the shift statements. You want the decimal number 209 to be entered as single digits in the sequence 2*100+0*10+9*1. Therefore you cannot use shift operations, which will multiply by powers of 2.
    You need to construct the INT representation from the input as follows:

    TOTAL = 100*S1+10*S2+S3;

    If your math library is too slow, you can speed up the operations for *10 and *100 by substituting it by

    Code:
    10_times(int x)
    {
    return x<<3 + x<1; /* x<<3 = x*8, x<<1 = x*2 */
    }
    
    100_times(int x)
    {
    return 10_times(x)*10_times(x)
    }
    
    You can also use macros (#define ...) to avoid the subroutine call and generate inline code instead.
     
  3. BHARGAV SHANKHALPARA

    BHARGAV SHANKHALPARA

    35
    0
    Nov 6, 2013
    hi
    Harald Kapp...

    Thank you so much...as you told i have done. its working correctly.

    Thanks again
     
  4. Harald Kapp

    Harald Kapp Moderator Moderator

    10,579
    2,359
    Nov 17, 2011
    I'm sorry, I detected a flaw in my code:
    Code:
    10_times(int x)
    {
    return x<<3 + x<1; /* x<<3 = x*8, x<<1 = x*2 */
    }
    
    
    100_times(int x)
    {
    /* wrong code :
    return 10_times(x)*10_times(x); -> this will give 100*x^2
    */
    /* correct code */
    return 10_times(10_times(x)); /* this will give 10*10*x = 100*x
    }
     
  5. BHARGAV SHANKHALPARA

    BHARGAV SHANKHALPARA

    35
    0
    Nov 6, 2013
    But i have done this way...

    TOTAL = 100*S1+10*S2+S3;

    and its working good...!!

    Thanks...
     
  6. kpatz

    kpatz

    317
    84
    Feb 24, 2014
    Fixed a typo in 10_times below:

    Code:
    10_times(int x)
    {
    return x<<3 + x<<1; /* x<<3 = x*8, x<<1 = x*2 */
    }
    
    100_times(int x)
    {
    return 10_times(x)*10_times(x)
    }
    
    You can also use macros (#define ...) to avoid the subroutine call and generate inline code instead.
     
  7. Harald Kapp

    Harald Kapp Moderator Moderator

    10,579
    2,359
    Nov 17, 2011
    yep, thanks

    Just for Bhargav's information: The typo is highlighted here in bold font:
    return x<<3 + x<<1;
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-