1 USING: help.markup help.syntax io math byte-arrays ;
4 ARTICLE: "stream-binary" "Working with binary data"
5 "Stream words on binary streams only read and write byte arrays. Packed binary integers can be read and written by converting to and from sequences of bytes. Floating point numbers can be read and written by converting them into a their bitwise integer representation (" { $link "floats" } ")."
7 "There are two ways to order the bytes making up an integer; " { $emphasis "little endian" } " byte order outputs the least significant byte first, and the most significant byte last, whereas " { $emphasis "big endian" } " is the other way around."
9 "Consider the hexadecimal integer "{ $snippet "HEX: cafebabe" } ". Little endian byte order yields the following sequence of bytes:"
11 { "Byte:" "1" "2" "3" "4" }
12 { "Value:" { $snippet "be" } { $snippet "ba" } { $snippet "fe" } { $snippet "ca" } }
14 "Compare this with big endian byte order:"
16 { "Byte:" "1" "2" "3" "4" }
17 { "Value:" { $snippet "ca" } { $snippet "fe" } { $snippet "ba" } { $snippet "be" } }
19 "Two words convert a sequence of bytes into an integer:"
22 "Two words convert an integer into a sequence of bytes:"
25 "Words for taking larger integers apart into smaller integers:"
28 { $subsection h>b/b } ;
30 ABOUT: "stream-binary"
33 { $values { "seq" "a sequence of bytes" } { "x" "a non-negative integer" } }
34 { $description "Converts a sequence of bytes in big endian order into an unsigned integer." } ;
37 { $values { "seq" "a sequence of bytes" } { "x" "a non-negative integer" } }
38 { $description "Converts a sequence of bytes in little endian order into an unsigned integer." } ;
41 { $values { "x" integer } { "n" "a non-negative integer" } { "b" "a byte" } }
42 { $description "Outputs the " { $snippet "n" } "th least significant byte of the sign-extended 2's complement representation of " { $snippet "x" } "." } ;
45 { $values { "x" integer } { "n" "a non-negative integer" } { "byte-array" byte-array } }
46 { $description "Converts an integer " { $snippet "x" } " into a string of " { $snippet "n" } " bytes in little endian order. Truncation will occur if the integer is not in the range " { $snippet "[-2^(8n),2^(8n))" } "." } ;
49 { $values { "x" integer } { "n" "a non-negative integer" } { "byte-array" byte-array } }
50 { $description "Converts an integer " { $snippet "x" } " into a string of " { $snippet "n" } " bytes in big endian order. Truncation will occur if the integer is not in the range " { $snippet "[-2^(8n),2^(8n))" } "." } ;
53 { $values { "x" integer } { "y" "a non-negative integer" } }
54 { $description "Masks off the least significant 8 bits of an integer." } ;
57 { $values { "d" "a 64-bit integer" } { "w1" "a 32-bit integer" } { "w2" "a 32-bit integer" } }
58 { $description "Outputs two integers, the least followed by the most significant 32 bits of the input." } ;
61 { $values { "w" "a 32-bit integer" } { "h1" "a 16-bit integer" } { "h2" "a 16-bit integer" } }
62 { $description "Outputs two integers, the least followed by the most significant 16 bits of the input." } ;
65 { $values { "h" "a 16-bit integer" } { "b1" "an 8-bit integer" } { "b2" "an 8-bit integer" } }
66 { $description "Outputs two integers, the least followed by the most significant 8 bits of the input." } ;