Shop OBEX P1 Docs P2 Docs Learn Events
ASCII to Decimal converter — Parallax Forums

ASCII to Decimal converter

science_geekscience_geek Posts: 247
edited 2009-07-22 02:48 in Propeller 1
does anyone have a simple ascii to decimal converter, i really dont want to have to spend the time to write one since i dont have much time to work with the prop. i know it wouldnt be hard, but i thought id ask.

Comments

  • JonnyMacJonnyMac Posts: 9,197
    edited 2009-07-22 02:45
    It's tragic that one who claims to be a "science geek" would deny themselves the learning opportunity to create an algorithm. Oh, well, your laziness is to my benefit -- I don't need this method now but I might need it later.

    [noparse][[/noparse] Edit ] : After futzing around with my own code I found a C function on the Internet that was more elegant and translated it to Spin.

    pub atol(pntr) | value, pos, c
    
    '' Converts string at pointer to signed long
    
      value := 0                                                    ' clear result
    
      if (byte[noparse][[/noparse]pntr] == "-")                                        ' if 1st char "-"
        pos := false                                                ' mark
        pntr++                                                      ' advance pointer
      else
        pos := true
    
      repeat while ((c := byte[noparse][[/noparse]pntr++]) > 0)                        ' get a character
        if (c => "0") and (c =< "9")                                ' valid?
          value := (value * 10) + (c - "0")                         ' yes, add to result
        else
          quit                                                      ' no, abort loop
    
      if pos
        return value
      else
        return -value
    

    Post Edited (JonnyMac) : 7/22/2009 2:34:23 PM GMT
  • W9GFOW9GFO Posts: 4,010
    edited 2009-07-22 02:48
    If you mean to simply display the decimal value of a character all you have to do is - "G" put the character in quotes.

    debug("G") will pass the decimal value of G, which is 71 to the debug method.

    Rich H
Sign In or Register to comment.