Thread: Conversion from ascii to decimal

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    4

    Conversion from ascii to decimal

    Hi there, need some help in my code here. My program suppose to display a number(decimal) on the screen via uart when I send a number but it is displaying in ascii form. I tried debugging it but there is no solution for it. When I send 88 it can only display X. I have attached the code. Please help me. I`m using MikroC compiler and i`m using a PIC.

    UART1_Write_Text("Ready to know your heartbeat ?? ");
    pulserate = 88;
    Temp = (pulserate - '0');
    newline();
    UART1_Write_Text("Your Heartbeat rate is : ");
    newline();
    UART1_Write(Temp);
    newline();

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion
    Lookup 88 in this table.

    To convert something like this, you need
    char1 = (pulserate / 10) + '0';
    char2 = (pulserate % 10) + '0';
    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.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    4
    The number 88 is only an example that I injected. How if the user key in some other random numbers like 100 or 58 ?? I need a constant code to automatically perform this calculations. Is it possible ?? Thank you.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Sure, just have /10 and %10 in a loop.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    4
    I did that but I get other symbols from the ascii table. Like for 100, I got the output :0. Is there anything that can be done ?? Thank you.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You keep saying "It can't be done", but you never post any code to show us just exactly in what way you're making a mess of it.

    And no, I'm not just going to give you the code.
    Yes, it is very easy to write.
    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.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    4
    I did the code. Please stop blaming that I did not write the code. I can show the you code. As a prove I did it. And this is the first time I`m using Mikro C and that is why I am asking for help. Thank you very much for your guidance all this while.

    UART1_Write_Text("Ready to know your heartbeat ?? ");
    Temp = pulserate ;
    char1 = (pulserate / 10) + '0';
    char2 = (pulserate % 10) + '0';
    char3 = (pulserate %100) + '0';
    newline();
    UART1_Write_Text("Your Heartbeat rate is : ");
    newline();
    UART1_Write(char1);
    UART1_Write(char2);
    UART1_Write(char3);
    newline();

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char3 = (pulserate %100) + '0';
    Try /100 instead.

    Also, you need to be careful in which order you output the chars.

    Consider a number like 123

    123 / 100 = 1
    123 / 10 = 12 (so mod this with 10) to get 2
    123 % 10 = 3
    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.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If your input varies in how many digits it is, then you're going to need a loop for it, and probably an array to store the characters.
    I'd perhaps look up the code for something like itoa if I were you. That's assuming that you don't have something available with your mikroC compiler libraries that does it for you.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error while computing ascii to decimal
    By abhishekcoder in forum C Programming
    Replies: 5
    Last Post: 04-08-2012, 11:38 AM
  2. decimal ascII character
    By byfreak in forum C++ Programming
    Replies: 3
    Last Post: 05-24-2008, 10:36 PM
  3. decimal ascII character
    By byfreak in forum C Programming
    Replies: 2
    Last Post: 05-24-2008, 06:37 PM
  4. how to convert decimal number to ASCII code?
    By oie in forum C Programming
    Replies: 11
    Last Post: 11-03-2006, 06:19 PM
  5. HEEEELP: Letters to ASCII decimal equivalents?
    By Mazerius in forum C++ Programming
    Replies: 7
    Last Post: 10-30-2002, 09:56 AM