# Binary with\_output\_to/2

**URL:** <https://swi-prolog.discourse.group/t/binary-with-output-to-2/6337>\
**Category:** PlDoc\
**Tags:** how-to\
**Created:** [March 3, 2023, 1:37am UTC](https://swi-prolog.discourse.group/t/binary-with-output-to-2/6337 "2023-03-03T01:37:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [March 3, 2023, 1:37am UTC](https://swi-prolog.discourse.group/t/binary-with-output-to-2/6337/1 "2023-03-03T01:37:37Z")

</div>

For some unit tests, I want to write to a binary list of codes. But it seems that `with_output_to(codes(Stream), ...)` does some kind of encoding (UTF8, I suppose).

I can make my unit test with tmp\_file\_stream/3 and the option `encoding(binary)`, but that’s a bit clumsy; it would be nice if I could set the encoding of a “codes” stream, but that gets the error “No permission to encoding stream ‘current\_output’”. (And a minor incompatibility: tmp\_file\_stream/3 has `encoding(binary)` but open/4, with\_output\_to/2, set\_stream/2 have `encoding(octet)`.)

Specifically, this is what I tried to do, and got an error:

```prolog
    with_output_to(codes(Codes),
                   ( set_stream(current_output, encoding(octet)),
                       ffi_write_int64(current_output, V)
                   )),

```

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [March 3, 2023, 8:47am UTC](https://swi-prolog.discourse.group/t/binary-with-output-to-2/6337/2 "2023-03-03T08:47:44Z")

</div>

Not sure this is useful enough to add the complications. Note that both memory files and temporary files can do what you need.

> [@peter.ludemann](#):
>
> And a minor incompatibility: [tmp\_file\_stream/3](https://www.swi-prolog.org/pldoc/doc_for?object=tmp_file_stream/3) has `encoding(binary)` but [open/4](https://www.swi-prolog.org/pldoc/doc_for?object=open/4), [with\_output\_to/2](https://www.swi-prolog.org/pldoc/doc_for?object=with_output_to/2), [set\_stream/2](https://www.swi-prolog.org/pldoc/doc_for?object=set_stream/2) have `encoding(octet)`.

tmp\_file\_stream/3 also accepts `encoding(octet)`. Not sure why it accepts `binary` as well. Might be history …

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [March 7, 2023, 5:10pm UTC](https://swi-prolog.discourse.group/t/binary-with-output-to-2/6337/3 "2023-03-07T17:10:56Z")

</div>

`library(memfile)` has memory\_file\_to\_codes/3, which allows specifying an encoding, which does just what I want with encoding `octet`.
