3

I try to use special loop variable I with [CHAR]. With this minimal example:

: ex ( -- )
    10 0 DO
    [CHAR] I EMIT
    LOOP
;

I expected '0123456789' to be printed but -obviously- I get 'IIIIIIIIII'

Is there a way to replace I with its value before [CHAR] gets executed?

2
  • 3
    No. Instead, to convert a value in the range 0-9 to the ASCII character code for the corresponding digit, add 48: I 48 + EMIT Commented Dec 14, 2024 at 11:24
  • @dave_thompson_085 I can't believe I didn't think about this! Thx. Commented Dec 14, 2024 at 12:48

1 Answer 1

3

[CHAR] X is a character literal, that is represented by a code point of "X" on the stack. The expression [CHAR] X always produces the same value on the stack.

If you want to print "1" given number 1 on the stack, you should get the code point of "1" at the first. You can do it as [char] 0 +.

So you example becomes:

: ex ( -- )
    10 0 DO
        I  [CHAR] 0 +  EMIT
    LOOP
;
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer! I would have accepted @dave_thompson_085's answer if he had offered one rather than a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.