]> gitweb.factorcode.org Git - factor.git/blob - core/io/binary/binary-docs.factor
Merge git://repo.or.cz/factor/jcg
[factor.git] / core / io / binary / binary-docs.factor
1 USING: help.markup help.syntax io math byte-arrays ;
2 IN: io.binary
3
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" } ")."
6 $nl
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."
8 $nl
9 "Consider the hexadecimal integer "{ $snippet "HEX: cafebabe" } ". Little endian byte order yields the following sequence of bytes:"
10 { $table
11     { "Byte:" "1" "2" "3" "4" }
12     { "Value:" { $snippet "be" } { $snippet "ba" } { $snippet "fe" } { $snippet "ca" } }
13 }
14 "Compare this with big endian byte order:"
15 { $table
16     { "Byte:" "1" "2" "3" "4" }
17     { "Value:" { $snippet "ca" } { $snippet "fe" } { $snippet "ba" } { $snippet "be" } }
18 }
19 "Two words convert a sequence of bytes into an integer:"
20 { $subsection be> }
21 { $subsection le> }
22 "Two words convert an integer into a sequence of bytes:"
23 { $subsection >be }
24 { $subsection >le }
25 "Words for taking larger integers apart into smaller integers:"
26 { $subsection d>w/w }
27 { $subsection w>h/h }
28 { $subsection h>b/b } ;
29
30 ABOUT: "stream-binary"
31
32 HELP: be>
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." } ;
35
36 HELP: le>
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." } ;
39
40 HELP: nth-byte
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" } "." } ;
43
44 HELP: >le
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))" } "." } ;
47
48 HELP: >be
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))" } "." } ;
51
52 HELP: mask-byte
53 { $values { "x" integer } { "y" "a non-negative integer" } }
54 { $description "Masks off the least significant 8 bits of an integer." } ;
55
56 HELP: d>w/w
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." } ;
59
60 HELP: w>h/h
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." } ;
63
64 HELP: h>b/b
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." } ;