Search code examples
forth

Forth - convert ASCII code to char


I try to write a program in Tachyon Forth for the Parallax Propeller microcontroller that reads data from the serial port.

The SERIN word returns the data as a number:

DECIMAL ok 57600 SERBAUD ok 31 SERIN . 36 ok

The "31 SERIN" reads data from pin P31 (RX). I typed "$" and got back 36. How can I convert this number (ASCII code) into a string with a single char? In this case with a single "$" sign.


Solution

  • Depends on what you want to do with the string. Here's one way to get a temporary string:

    : char-to-string ( c -- a u ) pad c!  pad 1 ;
    

    So e.g.

    36 char-to-string type
    $ ok