【问题标题】:Print binary data to stdout using Rebol使用 Rebol 将二进制数据打印到标准输出
【发布时间】:2015-06-02 11:28:38
【问题描述】:

我正在使用 Rebol 生成二进制输出,但输出不是我所期望的。 这是一个简单的测试脚本,打印从 0 到 255 的所有字节:

REBOL[]
for i 0 255 1 [
  prin to char! i
]

像这样执行测试:rebol -q test.rebol | hexdump -v

使用 Rebol 2.7,输出错过了 00 字节,但所有其他字节都很好:

0000000 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10
0000010 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20
...
00000e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef f0
00000f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff
00000ff

使用 Rebol 3 (r3-g25033f8),前 128 个字节没问题,但其余的都被更改了,似乎 Rebol3 将输出视为 UTF-8。

0000000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
0000010 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
...
0000060 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
0000070 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f
0000080 c2 80 c2 81 c2 82 c2 83 c2 84 c2 85 c2 86 c2 87
0000090 c2 88 c2 89 c2 8a c2 8b c2 8c c2 8d c2 8e c2 8f
...
0000160 c3 b0 c3 b1 c3 b2 c3 b3 c3 b4 c3 b5 c3 b6 c3 b7
0000170 c3 b8 c3 b9 c3 ba c3 bb c3 bc c3 bd c3 be c3 bf
0000180

有没有办法使用 Rebol 将二进制数据打印到标准输出?

【问题讨论】:

    标签: rebol rebol3 rebol2


    【解决方案1】:

    没有。在 Rebol 3 中,控制台 UTF-8,这就是 prin 产生的。

    但是,您可以将二进制文件写入一个文件,该文件在两个 Rebol 中都能完美运行。

    (我什至怀疑在某些系统上,您可以将二进制文件写入 %/dev/stdout,并得到您想要的。但我没有运行任何这些系统,所以请注意购买者。)

    【讨论】:

    • 我知道写二进制文件是可以的,但是给出了要求。我试图解决 mandelbrot benchmarksgame,它需要二进制输出:benchmarksgame.alioth.debian.org/u64q/…
    • 写入 %/dev/stdout 似乎确实可以在 OS X 上使用,尽管有一些警告(例如,使用 CALL/OUTPUT 从 Rebol 2 调用 Rebol 3 脚本会触发错误)
    【解决方案2】:

    在 Rebol 2 中,您可以使用 write-io 对 STDOUT 等端口进行纯粹的写入。

    所以你的例子应该是这样的:

    Rebol []
    
    for i 0 255 1 [
        write-io system/ports/output to-string to char! i 1
    ]
    

    Rebol 3 没有write-io,而是使用write,所以理论上您的示例应该如下所示:

    for i 0 255 1 [
        write system/ports/output to-string to char! i
    ]
    

    不幸的是,此时 system/ports/output 无法在 Rebol 3 中工作 :(

    $ r3
    
    >> probe system/ports/input
    make port! [
        spec: make object! [
            title: "Console Access"
            scheme: 'console
            ref: [scheme: 'console]
            path: none
        ]
        scheme: make object! [
            name: 'console
            title: "Console Access"
            spec: none
            info: none
            actor: make native! [[port!]]
            awake: none
        ]
        actor: make native! [[port!]]
        awake: none
        state: #{}
        data: none
        locals: none
    ]
    
    ;;  Good news is that STDIN is defined but...
    
    >> probe system/ports/output
    none
    == none
    
    ;; bad news is that STDOUT isnt :(
    

    【讨论】:

    • 谢谢。这似乎是解决方案。至少对于 Rebol2。如果在不久的将来没有更好的答案,我会接受。
    猜你喜欢
    • 2011-01-08
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 2012-04-14
    • 1970-01-01
    相关资源
    最近更新 更多