Search code examples
rebolrebol3rebol2

Print binary data to stdout using Rebol


I'm using Rebol to produce binary output, but the output is not what I expected. This is a simple test script, which prints all bytes from 0 to 255:

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

Execute the test like this: rebol -q test.rebol | hexdump -v

With Rebol 2.7, the output misses the 00 byte, but all other bytes are fine:

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

With Rebol 3 (r3-g25033f8), the first 128 byte is fine, but all the rest is altered, it seems like Rebol3 treats the output as 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

Is there any way to print binary data to the standard output using Rebol?


Solution

  • In Rebol 2 you can use write-io for unadulterated writing to ports like STDOUT.

    So your example would look like this:

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

    Rebol 3 doesn't have write-io and instead uses write so in theory your example should look something like this:

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

    Unfortunately at this moment system/ports/output isn't working in 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 :(