1 USING: byte-arrays help.markup help.syntax math
2 math.parser.private prettyprint namespaces make sequences
7 ARTICLE: "number-strings" "Converting between numbers and strings"
8 "These words only convert between real numbers and strings. Complex numbers are constructed by the parser (" { $link "parser" } ") and printed by the prettyprinter (" { $link "prettyprint" } ")."
10 "Integers can be converted to and from arbitrary bases. Floating point numbers can only be converted to and from base 10 and 16."
12 "Converting numbers to strings:"
20 "Converting strings to numbers:"
28 "You can also input literal numbers in a different base (" { $link "syntax-integers" } ")."
29 { $see-also "prettyprint-numbers" } ;
31 ABOUT: "number-strings"
34 { $values { "n" "an integer between 0 and 15" } { "ch" "a character" } }
35 { $description "Outputs a character representation of a digit." }
36 { $notes "This is one of the factors of " { $link number>string } "." } ;
39 { $values { "ch" "a character" } { "n" integer } }
40 { $description "Converts a character representation of a digit to an integer." }
41 { $notes "This is one of the factors of " { $link string>number } "." } ;
44 { $values { "str" string } { "radix" "an integer between 2 and 16" } { "n/f" { $maybe real } } }
45 { $description "Creates a real number from a string representation with the given radix. The radix for floating point literals can be either base 10 or base 16."
47 "Outputs " { $link f } " if the string does not represent a number." } ;
49 { >base base> } related-words
52 { $values { "str" string } { "n/f" { $maybe real } } }
53 { $description "Creates a real number from a string representation of a number in base 10."
55 "Outputs " { $link f } " if the string does not represent a number." } ;
57 { string>number number>string } related-words
60 { $values { "str" string } { "n/f" { $maybe real } } }
61 { $description "Creates a real number from a string representation of a number in base 2."
63 "Outputs " { $link f } " if the string does not represent a number." } ;
65 { >bin bin> .b } related-words
68 { $values { "str" string } { "n/f" { $maybe real } } }
69 { $description "Creates a real number from a string representation of a number in base 8."
71 "Outputs " { $link f } " if the string does not represent a number." } ;
73 { >oct oct> .o } related-words
76 { $values { "str" string } { "n/f" { $maybe real } } }
77 { $description "Creates a real number from a string representation of a number in base 16."
79 "Outputs " { $link f } " if the string does not represent a number." } ;
81 { >hex hex> .h } related-words
84 { $values { "n" real } { "radix" "an integer between 2 and 16" } { "str" string } }
85 { $description "Converts a real number into a string representation using the given radix. If the number is a " { $link float } ", the radix can be either base 10 or base 16." } ;
88 { $values { "n" real } { "str" string } }
89 { $description "Outputs a string representation of a number using base 2." } ;
92 { $values { "n" real } { "str" string } }
93 { $description "Outputs a string representation of a number using base 8." } ;
96 { $values { "n" real } { "str" string } }
97 { $description "Outputs a string representation of a number using base 16." }
100 "USING: math.parser prettyprint ;"
105 "USING: math.parser prettyprint ;"
112 { $values { "n" real } { "str" string } }
113 { $description "Converts a real number to a string." }
114 { $notes "Printing complex numbers requires the more general prettyprinter facility (see " { $link "prettyprint" } ")." } ;
117 { $values { "n" real } }
118 { $description "Appends the string representation of a real number to the end of the sequence being constructed by " { $link make } "." } ;
120 HELP: bytes>hex-string
121 { $values { "bytes" sequence } { "hex-string" string } }
122 { $description "Converts a sequence of bytes (integers in the range [0,255]) to a string of hex numbers in the range [00,ff]." }
124 { $example "USING: math.parser prettyprint ;" "B{ 1 2 3 4 } bytes>hex-string ." "\"01020304\"" }
126 { $notes "Numbers are zero-padded on the left." } ;
128 HELP: hex-string>bytes
129 { $values { "hex-string" sequence } { "bytes" byte-array } }
130 { $description "Converts a sequence of hex numbers in the range [00,ff] to a sequence of bytes (integers in the range [0,255])." }
132 { $example "USING: math.parser prettyprint ;" "\"cafebabe\" hex-string>bytes ." "B{ 202 254 186 190 }" }
135 { bytes>hex-string hex-string>bytes } related-words