使用 Forth 解码以十六进制编码的消息

Decode a message encoded in hexadecimal, with Forth

我试图在 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!

给定问题的可重用单元是 decode-hex-string ( a1 u1 a2 u2 -- a2 u ) 将字符串 ( a1 u1 ) 转换为缓冲区 ( a2 u2 ) 和 returns 字符串(或一般情况下的二进制数据)的单词 ( a2 u )。实际上,string 和 buffer 几乎是一样的:string 是一个包含文本数据的缓冲区。我们还假设 char 大小和地址单元大小为 1 个字节。

: b! c! ; \ store byte in case of 1 char size is 1 bite

: s-to-n ( addr u base -- x ) \ convert string into number with the given radix
  base @ >r base !
  0. 2swap >number ( d a2 u2 )
  r> base !
  nip or if -11 throw then
;
: hex-to-number ( a1 u1 -- x ) 16 s-to-n ;

: decode-hex-string ( a1 u1 a2 u2 -- a2 u )
  rot 2/ umin 2dup 2>r ( a1 a2 u )
  over + swap ?do dup 2 hex-to-number i b! 2 + loop drop 2r>
;

\ test
s" 48656C6C6F20776F726C6421" here 100 decode-hex-string cr type cr