Thread: Typecasting Problem

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    56

    Typecasting Problem

    Hi,

    I would like to convert an ADC 0-5V reading into a percentage value 0-100%. I have tried a couple of attempts below, and also tried in a flat C compiler using printf and I still get zero.

    Can you see any errors with my c syntax? I think it does not like typecasting float numbers. I have also tried typecasting into a float variable, and printing in flat c, but this also does not work. Any advice would be gladly accepted.

    Thanks,

    RocketMan46

    Code:
    //************* attempt 1 ***********
    
    int adc_value = 255;
    unsigned char = result;
    
    result = (unsigned char)((adc_value/1023)*100); // this returns zero
    
    //************* attempt 2 ***********
    
    int adc_value = 255;
    unsigned char = result;
    
    result = (unsigned char)(adc_value/1023)*100; // this returns zero
    
    //************* attempt 3 in flat c ***********
    
    int adc_value = 255;
    unsigned char = result;
    
    result = (unsigned char)((adc_value/1023)*100);
    
    printf("%d", result); // this returns zero
    
    //************* attempt 4 in flat c ***********
    
    unsigned char = result;
    
    result = (unsigned char)((10/10)*100);
    
    printf("%d", result); // this returns 100 which is correct

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Nowhere do you actually try

    (float)adc_value
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Typecasting
    By ~Kyo~ in forum C++ Programming
    Replies: 3
    Last Post: 05-10-2011, 05:56 PM
  2. typecasting
    By eklavya8 in forum C Programming
    Replies: 9
    Last Post: 01-03-2009, 09:02 AM
  3. typecasting
    By sreetvert83 in forum C++ Programming
    Replies: 7
    Last Post: 07-22-2005, 01:55 PM
  4. typecasting
    By v6sa in forum C++ Programming
    Replies: 2
    Last Post: 05-08-2005, 04:01 AM
  5. Problem with Typecasting
    By pinkcheese in forum C++ Programming
    Replies: 4
    Last Post: 04-14-2002, 03:26 AM

Tags for this Thread