-
If I have some bytes in a normal list, what is the best way to write them to a stream? I suppose there must be something better than just calling
This example fails, because Is there something am I missing or do we need to add a new predicate? |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 3 replies
-
The best way is to represent bytes as a list of characters with codes in range 0..255. In this way, you can use the efficient internal representation of Scryer Prolog. You can use for example You should not use See also #1072. |
Beta Was this translation helpful? Give feedback.
-
Using For efficiency, the most important point is to write as many bytes as possible "at once", and hence to avoid many successive writes of individual bytes. If you use |
Beta Was this translation helpful? Give feedback.
-
What is the type of these byte-chars? They are a subset of |
Beta Was this translation helpful? Give feedback.
-
What about |
Beta Was this translation helpful? Give feedback.
-
@triska: what about |
Beta Was this translation helpful? Give feedback.
-
Since we already have |
Beta Was this translation helpful? Give feedback.
-
In any case, |
Beta Was this translation helpful? Give feedback.
-
I have filed #1587 to add this! |
Beta Was this translation helpful? Give feedback.
The best way is to represent bytes as a list of characters with codes in range 0..255. In this way, you can use the efficient internal representation of Scryer Prolog. You can use for example
format(Stream, "~s", [Cs])
to write these characters to a binary stream.format/3
will automatically interpret them as bytes ifStream
is binary.You should not use
'$put_chars/'2
directly. You can convert lists of bytes (integers in 0..255)Bs
to lists of charactersCs
withchar_code/2
:maplist(char_code, Cs, Bs)
. Ideally, this is avoided, and characters are used throughout.See also #1072.