Non-decimal radices/Input: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 189: | Line 189: | ||
100 in base 10 is 100 in base 10 |
100 in base 10 is 100 in base 10 |
||
100 in base 16 is 256 in base 10</lang> |
100 in base 16 is 256 in base 10</lang> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
<lang cpp>#include <iostream> |
<lang cpp>#include <iostream> |
||
Line 218: | Line 219: | ||
If <code>:radix</code> is omitted, it defaults to 10. If <code>:junk-allowed</code> is omitted, it defaults to <code>nil</code>, causing <code>#'parse-integer</code> to signal an error of type <code>parse-error</code> rather than just returning <code>nil</code> whenever the input string isn't a numeral possibly surrounded by whitespace. |
If <code>:radix</code> is omitted, it defaults to 10. If <code>:junk-allowed</code> is omitted, it defaults to <code>nil</code>, causing <code>#'parse-integer</code> to signal an error of type <code>parse-error</code> rather than just returning <code>nil</code> whenever the input string isn't a numeral possibly surrounded by whitespace. |
||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
Line 316: | Line 318: | ||
( scratchpad ) "11.1101" bin> . |
( scratchpad ) "11.1101" bin> . |
||
11.1101 |
11.1101 |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
Arbitrary base 2-36 parsing is supported by the same mechanism as [[User Input#Forth|decimal parsing]]: set the user variable BASE to the desired base, then scan the number. There are two convenience words for setting the base to DECIMAL or HEX. |
Arbitrary base 2-36 parsing is supported by the same mechanism as [[User Input#Forth|decimal parsing]]: set the user variable BASE to the desired base, then scan the number. There are two convenience words for setting the base to DECIMAL or HEX. |
||
Line 764: | Line 767: | ||
print oct($bin_withprefix), "\n"; # => 345 |
print oct($bin_withprefix), "\n"; # => 345 |
||
# nothing for binary without prefix</lang> |
# nothing for binary without prefix</lang> |
||
=={{header|Perl 6}}== |
|||
By default, all strings of digits are parsed as base 10 numbers, including those with a leading zero. Numbers with a prefix 0b, 0o, 0d or 0x are parsed as binary, octal, decimal or hexadecimal respectively. |
|||
<lang perl6>say 0b11011; # -> 27 |
|||
say 0o11011; # -> 4617 |
|||
say 0d11011; # -> 11011 |
|||
say 0x11011; # -> 69649</lang> |
|||
Additionally, there are built-in adverbial prefix operators to parse strings of "digits" of radix 2 through radix 36 into decimal. They will fail with a runtime error if they are fed a digit that is not valid in that radix. |
|||
<lang perl6>my $n = '11011'; |
|||
say :2($n); # -> 27 |
|||
say :3($n); # -> 112 |
|||
say :4($n); # -> 325 |
|||
say :5($n); # -> 756 |
|||
say :6($n); # -> 1519 |
|||
say :7($n); # -> 2752 |
|||
say :8($n); # -> 4617 |
|||
say :9($n); # -> 7300 |
|||
say :10($n); # -> 11011 |
|||
say :11($n); # -> 15984 |
|||
say :12($n); # -> 22477 |
|||
say :13($n); # -> 30772 |
|||
say :14($n); # -> 41175 |
|||
say :15($n); # -> 54016 |
|||
say :16($n); # -> 69649 |
|||
say :17($n); # -> 88452 |
|||
say :18($n); # -> 110827 |
|||
say :19($n); # -> 137200 |
|||
say :20($n); # -> 168021 |
|||
say :21($n); # -> 203764 |
|||
say :22($n); # -> 244927 |
|||
say :23($n); # -> 292032 |
|||
say :24($n); # -> 345625 |
|||
say :25($n); # -> 406276 |
|||
say :26($n); # -> 474579 |
|||
say :27($n); # -> 551152 |
|||
say :28($n); # -> 636637 |
|||
say :29($n); # -> 731700 |
|||
say :30($n); # -> 837031 |
|||
say :31($n); # -> 953344 |
|||
say :32($n); # -> 1081377 |
|||
say :33($n); # -> 1221892 |
|||
say :34($n); # -> 1375675 |
|||
say :35($n); # -> 1543536 |
|||
say :36($n); # -> 1726309</lang> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 853: | Line 810: | ||
echo +"07651", "\n"; // prints 7651 |
echo +"07651", "\n"; // prints 7651 |
||
?></lang> |
?></lang> |
||
=={{header|PL/I}}== |
|||
<lang PL/I>declare N fixed binary; |
|||
get edit (N) (A(7)); /* decimal input of 7 columns */ |
|||
put skip list (N); |
|||
declare BS bit (32); |
|||
get edit (BS) (B(32)); /* Binary input of 32 binary digits. */ |
|||
put skip edit (BS) (B);</lang> |
|||
<pre> |
|||
23 |
|||
11010101010111111110000000011101 |
|||
</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
Line 879: | Line 823: | ||
Output: |
Output: |
||
<pre>123456789012345678901234567890</pre> |
<pre>123456789012345678901234567890</pre> |
||
=={{header|PL/I}}== |
|||
<lang PL/I>declare N fixed binary; |
|||
get edit (N) (A(7)); /* decimal input of 7 columns */ |
|||
put skip list (N); |
|||
declare BS bit (32); |
|||
get edit (BS) (B(32)); /* Binary input of 32 binary digits. */ |
|||
put skip edit (BS) (B);</lang> |
|||
<pre> |
|||
23 |
|||
11010101010111111110000000011101 |
|||
</pre> |
|||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
Line 982: | Line 939: | ||
</pre> |
</pre> |
||
=={{ |
=={{header|PureBasic}}== |
||
<lang PureBasic> ;Val() parses integer strings |
<lang PureBasic> ;Val() parses integer strings |
||
; decimal numbers have no prefix, hexadecimal needs a prefix of '$', binary needs a prefix of '%' |
; decimal numbers have no prefix, hexadecimal needs a prefix of '$', binary needs a prefix of '%' |
||
Line 1,071: | Line 1,028: | ||
;; -> '(123 123 123 123 123 123 123 123) |
;; -> '(123 123 123 123 123 123 123 123) |
||
</lang> |
</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
By default, all strings of digits are parsed as base 10 numbers, including those with a leading zero. Numbers with a prefix 0b, 0o, 0d or 0x are parsed as binary, octal, decimal or hexadecimal respectively. |
|||
<lang perl6>say 0b11011; # -> 27 |
|||
say 0o11011; # -> 4617 |
|||
say 0d11011; # -> 11011 |
|||
say 0x11011; # -> 69649</lang> |
|||
Additionally, there are built-in adverbial prefix operators to parse strings of "digits" of radix 2 through radix 36 into decimal. They will fail with a runtime error if they are fed a digit that is not valid in that radix. |
|||
<lang perl6>my $n = '11011'; |
|||
say :2($n); # -> 27 |
|||
say :3($n); # -> 112 |
|||
say :4($n); # -> 325 |
|||
say :5($n); # -> 756 |
|||
say :6($n); # -> 1519 |
|||
say :7($n); # -> 2752 |
|||
say :8($n); # -> 4617 |
|||
say :9($n); # -> 7300 |
|||
say :10($n); # -> 11011 |
|||
say :11($n); # -> 15984 |
|||
say :12($n); # -> 22477 |
|||
say :13($n); # -> 30772 |
|||
say :14($n); # -> 41175 |
|||
say :15($n); # -> 54016 |
|||
say :16($n); # -> 69649 |
|||
say :17($n); # -> 88452 |
|||
say :18($n); # -> 110827 |
|||
say :19($n); # -> 137200 |
|||
say :20($n); # -> 168021 |
|||
say :21($n); # -> 203764 |
|||
say :22($n); # -> 244927 |
|||
say :23($n); # -> 292032 |
|||
say :24($n); # -> 345625 |
|||
say :25($n); # -> 406276 |
|||
say :26($n); # -> 474579 |
|||
say :27($n); # -> 551152 |
|||
say :28($n); # -> 636637 |
|||
say :29($n); # -> 731700 |
|||
say :30($n); # -> 837031 |
|||
say :31($n); # -> 953344 |
|||
say :32($n); # -> 1081377 |
|||
say :33($n); # -> 1221892 |
|||
say :34($n); # -> 1375675 |
|||
say :35($n); # -> 1543536 |
|||
say :36($n); # -> 1726309</lang> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 1,226: | Line 1,230: | ||
bases.foreach(base => println(f"String $s in base $base%2d is $BigInt(s, base)%5d")) |
bases.foreach(base => println(f"String $s in base $base%2d is $BigInt(s, base)%5d")) |
||
}</lang> |
}</lang> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
<lang scheme>> (string->number "abcf123" 16) ; hex |
<lang scheme>> (string->number "abcf123" 16) ; hex |