Shop OBEX P1 Docs P2 Docs Learn Events
Converting a ASCII number to a character — Parallax Forums

Converting a ASCII number to a character

PhilldapillPhilldapill Posts: 1,283
edited 2008-12-28 00:46 in Propeller 1
I'm using the Keyboard object and in particular, the getKey routine. It returns a number between 0 and 255, cooresponding to the key pressed. How would I go about converting this number to an actual character? I've thought about using a lookup table, and if that's the case, how do I concatenate the single character, with a string? I want to be able to allow the user to input a filename.

Comments

  • mynet43mynet43 Posts: 644
    edited 2008-12-27 23:36
    Here's an example:

      if kbdflag
        repeat until char == $CB            ' ESC key to exit keypress routine
          char := vga.getkey                ' get a key from the keyboard
          vga.out(char)                     ' output the character to the screen
    
    



    I've attached the routine VGA_1024. If you look at the code there, you'll see the use if getkey and the routine "out" that prints the characters to the screen.

    Let me know if you have any questions.

    Jim
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-12-27 23:53
    Thanks Jim. However, I am trying to take each character that is pressed, and add it into an array. What I have in mind, is to declare something like:
    VAR
    byte FileName[noparse][[/noparse]32]

    When a key is pressed, it will be checked to see if it is a letter, number, or other acceptable character for a filename. If it is, I will add it onto the end of this array. When the user hits the Enter key, this array will then somehow be translated into a string, and passed to the SD Card object, and a file will be saved with that name. The trouble is, how do you do something as simple as this...
  • mynet43mynet43 Posts: 644
    edited 2008-12-28 00:00
    OK, here's an example I just typed in. I haven't tested it but it should give you an idea:

    VAR
      byte array[noparse][[/noparse]80], char
      long i
    
    ' here's the code
    
      i := char := 0
      
      repeat while char <> $0D              ' stop when return char input
      
        char := vga.getkey
        vga.out(char)
        
        if char <> $0D
          array[noparse][[/noparse]i++] := char
        else
          array[i] := 0                     ' terminate string with zero
    [/i]
    



    Hope this helps.

    Jim
  • mynet43mynet43 Posts: 644
    edited 2008-12-28 00:02
    Phil,

    The last line should be: array sub i := 0. For some reason, the forum is removing the character sequence[noparse]:([/noparse]
  • soshimososhimo Posts: 215
    edited 2008-12-28 00:34
    code looks good jim, and I think the reason the forum gets confused is because [noparse][[/noparse] i ] (without spaces) is the forum markup for an image tag. If i is used as a counting variable this happens every time I'll bet when code is posted on the forums.

    One thing I would add to the code, however, is a check to see if the character is a valid character or not. That can be done with a simple compare. Also, you probably want to add a check to make sure that input doesn't overflow your buffer. So I would change the repeat line to

    repeat while (char <> $0d and i < 80)
    
    

    Post Edited (soshimo) : 12/28/2008 12:41:42 AM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-28 00:42
    Both FemtoBasic and the Propeller OS have routines for scanning file names. FemtoBasic's is called scanFilename and is around line #600. This expects the end of a name to be marked by a double quote and copies the name to a 32 byte array whose address is passed and adds a zero byte at the end as expected by the SD card routines. Here's a generalized version:
    pri scanFilename(f, tp, len, delim) | c, chars
    ' f is the address of the result string.  It's length is given as len
    ' tp is the address of the input string
    ' len is the length of the result string including the terminating zero
    ' delim is the end of string character.  This would be $0D for return
    ' This function returns the address after the end of string character.
       chars := 0
       repeat while (c := byte[noparse][[/noparse]tp++]) <> delim
          if chars++ < len-1
             byte[noparse][[/noparse]f++] := c
       byte[noparse][[/noparse]f] := 0
       return tp
    

    Post Edited (Mike Green) : 12/28/2008 12:51:11 AM GMT
  • mynet43mynet43 Posts: 644
    edited 2008-12-28 00:46
    Thanks for the feedback. You're right on all counts...

    I was just trying to show the approach.

    Regarding the [noparse][[/noparse] i ] being deleted by the forum, I think this may be OK for text input. But I think it is a definite BUG when it happens within the "code" directive.

    Does anyone know who to talk to to get this fixed? It's kinda frustrating when you're trying to post some code. Using "i" as an index probably happens more often than any other [noparse]:)[/noparse]

    Jim
Sign In or Register to comment.