我试图在 Forth 中转换以十六进制编码的消息。下面三个字就出来了。它可以工作,但看起来很丑陋,而且不必要地复杂。
此外,这不是一个真正的解决方案,因为我希望将结果存储在另一个字符串中以进行进一步操作(而不是像现在这样在屏幕上键入),但我不知道如何添加一个数字(代表一个字符) 到一个地址...
那么,即使是很长的消息,您认为必须如何做到这一点?什么是最好的方法?
\ This work on Gforth 0.7.3
: print ( addr u -- ) hex evaluate decimal emit ;
\ s" 48" print -> H
: hex-decode-char ( addr u n -- ) >r drop r> 2 * + 2 print ;
\ s" 48656C6C6F20776F726C6421" 0 hex-decode-char -> H
: hex-decode-string ( addr u -- ) 2dup dup 2 / 0 ?do i hex-decode-char 2dup loop 2drop 2drop ;
\ s" 48656C6C6F20776F726C6421" hex-decode-string -> Hello world!