Post Reply 
HP 50g Binary Number Formatting
04-24-2022, 06:06 PM (This post was last modified: 04-24-2022 08:22 PM by dlidstrom.)
Post: #10
RE: HP 50g Binary Number Formatting
Here’s an implementation that accepts a real and converts it into a string.

→BIN4: r → s

Code:

« → d
  « { }
    DO d 16 MOD → r
      «
        CASE
        'r==0' THEN "0000" END
        'r==1' THEN "0001" END
        'r==2' THEN "0010" END
        'r==3' THEN "0011" END
        'r==4' THEN "0100" END
        'r==5' THEN "0101" END
        'r==6' THEN "0110" END
        'r==7' THEN "0111" END
        'r==8' THEN "1000" END
        'r==9' THEN "1001" END
        'r==10' THEN "1010" END
        'r==11' THEN "1011" END
        'r==12' THEN "1100" END
        'r==13' THEN "1101" END
        'r==14' THEN "1110" END
        'r==15' THEN "1111" END
        END
      »
      + d 16 / IP 'd' STO
    UNTIL
      'd==0'
    END
  »
  IF
    DUP SIZE 1 >
  THEN
    REVLIST
    « " " SWAP + + »
    STREAM
  ELSE
    EVAL
  END
»

This pattern matching approach trades some bytes for readability, which I tend to favour these days (when programming in other languages). I didn’t find a string join function that could avoid the last if statement. Unfortunately STREAM errors on a list with a single element.

2xHP48GX, HP 50g, two Retrotronik ram cards, DM42
/Daniel Lidström
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
HP 50g Binary Number Formatting - aaron - 04-20-2022, 05:47 PM
RE: HP 50g Binary Number Formatting - Gil - 04-23-2022, 01:38 PM
RE: HP 50g Binary Number Formatting - Gil - 04-23-2022, 06:01 PM
RE: HP 50g Binary Number Formatting - Gil - 04-23-2022, 06:50 PM
RE: HP 50g Binary Number Formatting - Gil - 04-24-2022, 12:03 AM
RE: HP 50g Binary Number Formatting - 3298 - 04-24-2022, 10:40 AM
RE: HP 50g Binary Number Formatting - Gil - 04-24-2022, 11:34 AM
RE: HP 50g Binary Number Formatting - dlidstrom - 04-24-2022 06:06 PM
RE: HP 50g Binary Number Formatting - Gil - 04-25-2022, 04:04 PM
RE: HP 50g Binary Number Formatting - Gil - 04-25-2022, 10:34 PM



User(s) browsing this thread: 3 Guest(s)