1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Converting ASCII to numbers

  1. Oct 14, 2012 #1
    1. The problem statement, all variables and given/known data

    In assembly, data is usually input in the form of ASCII character strings and it is then converted to the appropriate form for processing.

    a) Explain how you would do to convert an ASCII character representing a decimal digit (i.e. ‘0’, ‘1’, …, ‘9’) into an actual decimal value

    b) Extend your answer in (a) by explaining how you would do to convert ASCII character strings to whole integer numbers (i.e. 10s, 100s, 100s, …).

    c) Considering the fact that each ASCII character is stored on 8 bits, explain how you would do to fit your converted number in (c) into a 32-bit word.


    3. The attempt at a solution

    A. The hex values for each ASCII character representing a decimal number range from 0x30 to 0x39. We take this hex value and subtract 0x30 from it to achieve the proper decimal value. For example, the ascii value '1' is 0x31, so 0x31 – 0x30 = 0x01 = decimal value 1.

    B. These are the steps you would take to convert a string to a number.

    Start with a stored final result of 0.
    Loop through the string starting at the most significant digit.
    1. Multiply the stored result by 10.
    2. Subtract 0x30 from the current ascii digit and add the result to the final result.
    3. Move to the next digit in the string and start the process over at step 1.
    4. Return with the final result if there are no more ascii digits.

    C. I don't understand this question.... If you're converting, say, "1234" into a number then it should fit easily into a 32 bit word....
     
  2. jcsd
  3. Oct 14, 2012 #2

    rcgldr

    User Avatar
    Homework Helper

    I think your logic is the answer for part c. Part b is confusing, is it asking you to store each digit (0 through 9) in separate memory locations, so you just end up with yet another string of bytes, but in the range 0 through 9 versus hex 30 through hex 39? For part b, you could store the digits as BCD (packed decimal), 4 bits per digit, but I doubt that is the intent of part b.
     
  4. Oct 14, 2012 #3
    I was a little confused because the only way you can convert a string to a number (to my knowledge) is with the aid of a loop.

    However, my solution to B does work... I just don't understand how it ties into C.
     
  5. Oct 14, 2012 #4

    I like Serena

    User Avatar
    Homework Helper

    I think in (c) they want you to consider that you should not store intermediate results in an 8-bit character.
     
  6. Oct 14, 2012 #5

    rcgldr

    User Avatar
    Homework Helper

    You solution is really a solution to C. It seems that B just wants you to convert a string of ASCII digits into a string of bytes that range from 0 to 9, although you'd need to define some value as a terminator for the string of bytes. Perhaps B is just an intermediate step that you're just supposed to think about without actually implementing it.
     
  7. Oct 14, 2012 #6
    Would it be possible that C is referring to overflow conditions (ie. if you're converting a string that has over 10 digits then you are definitely going to overflow)?
     
  8. Oct 14, 2012 #7

    rcgldr

    User Avatar
    Homework Helper

    I'm not sure, but B implies separate integer numbers for 10's, 100's, ... .
     
  9. Oct 14, 2012 #8
    That seems awfully inefficient... Having an array which stores each integer representing 10's, 100's, etc. would be rather wasteful wouldn't it?
     
  10. Oct 14, 2012 #9

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Yes, it would be inefficient. That's not the point. This is a programming exercise where the student is to build up capabilities piece by piece. The intent is to aid the student's understanding.
     
  11. Oct 15, 2012 #10
    I just don't think it's right though... We haven't discussed arrays in class very much.

    The only way i can see C making sense is if i store each individual converted number in 8 bits. In which case the answer to C would be to simply add each 8-bit number together in order for it to fit into a 32 bit word. I think.
     
  12. Oct 15, 2012 #11

    rcgldr

    User Avatar
    Homework Helper

    You already solved C. My impression of B is that it's just asking what a string of ascii digits represents, and what you should do with the digits, such as multipy the 10's digit by 10, multiply the 100's digit by 100, and then C asks how to create a loop that does this while converting a string into a 32 bit binary number. I don't think B is actually asking for a code fragment that stores the digits into an array.
     
  13. Oct 15, 2012 #12
    Ohhh, I see what you mean...
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook