Shop OBEX P1 Docs P2 Docs Learn Events
Need to concatenate a string, #1+#2+#3=#123. Help! — Parallax Forums

Need to concatenate a string, #1+#2+#3=#123. Help!

laser-vectorlaser-vector Posts: 118
edited 2010-11-06 02:10 in Propeller 1
[EDIT] i changed the title of this thread just because i didnt feel starting a new one was necessary. original title was: "Need a way to take 4 numarical ASCII chars and convert to a real number" the new issue im having is that i need to take and array of bytes

eg. Byte Variable[2]
Variable[0] = 1
Variable[1] = 2
Variable[2] = 3

and concatenate them into one Long so that the long equals the actual number #123


[original thread]

lets say im using a terminal program and i enter 1234,

how could i convert %00110001_00110010_00110011_00110100 (or ascii 1234)
to the actual number %00000000_00000000_00000100_11010010 (or #1234)??

only way i can think is to make a lookup table.. is there any easier methods anyone knows about?

Thanks!!!

Comments

  • laser-vectorlaser-vector Posts: 118
    edited 2010-11-06 00:40
    ok so i've got something going.. but still need a little help

    so think of N.Byte[n] as an array..

    N.byte[0] = 1
    N.byte[1] = 2
    N.byte[2] = 3
    N.byte[3] = 4

    but now im a little stumped, any ideas on how to take 1, 2, 3 and 4 and make them = 1234??


    heres the code by the way:
    PUB ASCIIToNum(bytecnt) | C, N
            C := 0
            data := 0         
            Repeat         
              RXbyte := Debug.RXcheck            
              If RXbyte => 0
                Case RXbyte
                  N0[0] :
                    N.byte[C] := N0[1]
                  N1[0] :
                    N.byte[C] := N1[1]
                  N2[0] :
                    N.byte[C] := N2[1]
                  N3[0] :
                    N.byte[C] := N3[1]
                  N4[0] :
                    N.byte[C] := N4[1]
                  N5[0] :
                    N.byte[C] := N5[1]
                  N6[0] :
                    N.byte[C] := N6[1]
                  N7[0] :
                    N.byte[C] := N7[1]
                  N8[0] :
                    N.byte[C] := N8[1]
                  N9[0] :
                    N.byte[C] := N9[1]
                  $0D   :
                    c := (bytecnt - 1)
                 other :
                   Debug.Str(String("Error, not a valid number character!!"))
                   return
                   
                if c => (bytecnt - 1)               
                   ????????Dont know what to do here??????               
                   return
                         
               C++      
    DAT
    
          N0      byte  $30, $00
          N1      byte  $31, $01
          N2      byte  $32, $02
          N3      byte  $33, $03
          N4      byte  $34, $04
          N5      byte  $35, $05
          N6      byte  $36, $06
          N7      byte  $37, $07
          N8      byte  $38, $08
          N9      byte  $39, $09
    
  • kuronekokuroneko Posts: 3,623
    edited 2010-11-06 00:52
    What about this? The function takes a string (assuming ["0"-"9"] only and length within long range) and returns the numeric result.
    PRI cnv(addr) | c
    
      repeat while c := byte[addr++]
        result := result * 10 + (c - "0")
    
  • laser-vectorlaser-vector Posts: 118
    edited 2010-11-06 01:23
    Hey thanks for the reply! im having a little trouble with that example, maybe its just something im doing..

    either way you did give me an idea for a better way to collect those ascii bytes and convert them to actual numbers.
    VAR
    
            byte AsciiNumbers[9] 
    
    PUB ASCIIToNum(DecimalCount) | C, N
    
            Repeat C from 0 to 9
              AsciiNumbers[c] := 0
            C := 0
            data := 0                   
            Repeat         
              RXbyte := Debug.RXcheck            
              If RXbyte => 0
                case RXbyte
                  $30..$39 :
                    AsciiNumbers[c] := RXbyte - $30
                  $0D   :
                    c := (DecimalCount - 1)
                 other :
                   Debug.Str(String("ERROR"))
                   return                 
                if c => (DecimalCount - 1)
                   repeat c from 0 to (DecimalCount - 1)           
                     Debug.str(num.tostr(AsciiNumbers[c], num#hex))                
                   return                     
               C++
    

    that is so much smaller and can read more ascii bytes thus making much larger numbers!

    but im still stumped on how to make 1 + 2 = 12???????????
  • kuronekokuroneko Posts: 3,623
    edited 2010-11-06 01:38
    What about this then? The function reads characters from serial-in and aborts conversion for any non-numerical character.
    CON
      _clkmode = XTAL1|PLL16X
      _xinfreq = 5_000_000
      
    OBJ
      Debug: "FullDuplexSerial"
    
    PUB null
    
      Debug.start(31, 30, %0000, 115200)
      waitcnt(clkfreq*5 + cnt)
    
      repeat
        Debug.dec(cnv)
        Debug.tx(13)
      
    PUB cnv | RXbyte
    
      repeat               
        RXbyte := Debug.RX                       
        case RXbyte
          "0".."9":
            result := result * 10 + (RXbyte - "0")
          other:
            return
    
    DAT
    
  • laser-vectorlaser-vector Posts: 118
    edited 2010-11-06 01:48
    @ kuroneko

    hey man thanks for the input, the code you posted looks oddly familiar to the code i just posted, proof that great minds do think alike! :D lol
    [edit] well it did look similar for a second there, but i guess you're currently editing it :P

    its not the conversion of an ascii byte to a single digit number thats causing me grief, its more or less the process of concatenating those collected bytes into a number inside of a Long

    eg. #1 + #2 + #3 + #4 = #1234 (not #10)
  • kuronekokuroneko Posts: 3,623
    edited 2010-11-06 01:54
    It's not rocket science! ;) The red line is executed 4 times for an input of "1234" + CR.
    PUB cnv | RXbyte
    
      repeat               
        RXbyte := Debug.RX      ' blocking call                 
        case RXbyte
          "0".."9":
            [COLOR="Red"]result := result * 10 + (RXbyte - "0")[/COLOR]
          other:
            return
    
    1. result starts with 0 and will now be 0 * 10 + 1 = 1
    2. result = 1 * 10 + 2 = 12
    3. result = 12 * 10 + 3 = 123
    4. result = 123 * 10 + 4 = 1234
    5. loop breaks (CR is not in [0..9]) and result is returned as 1234
    the code you posted looks oddly familiar to the code i just posted
    I just re-used it for convenience.
  • laser-vectorlaser-vector Posts: 118
    edited 2010-11-06 02:10
    Ok i gotcha, yea i was really over complicating this! lol

    i must have been looking at this from a totally different angle and didnt grasp the simplicity of your example untill i actually plugged it in and gave it a go.

    guess thats what sleep deprivation, 5am, no coffee and a boss whos giving unreal deadlines will do to a fella..

    Thank you!!
Sign In or Register to comment.