Skip to content

Best way to write some bytes to a Stream #1555

Answered by triska
aarroyoc asked this question in Q&A
Discussion options

You must be logged in to vote

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 if Stream is binary.

You should not use '$put_chars/'2 directly. You can convert lists of bytes (integers in 0..255) Bs to lists of characters Cs with char_code/2: maplist(char_code, Cs, Bs). Ideally, this is avoided, and characters are used throughout.

See also #1072.

Replies: 8 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@aarroyoc
Comment options

aarroyoc Aug 11, 2022
Author Sponsor

Answer selected by aarroyoc
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@triska
Comment options

@UWN
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants