Non-decimal radices/Convert: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: az only) |
m (→{{header|Phix}}: added/tested 0(16) example) |
||
Line 2,350: | Line 2,350: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Phix itself handles number input in the expected decimal, or binary, octal, hexadecimal, and any base from 2 to 36 using prefixes 0b/0o/t/ |
Phix itself handles number input in the expected decimal, or binary, octal, hexadecimal, and any base from 2 to 36 using prefixes 0b/0o/t/0x/X/# and 0(2..36)<br> |
||
The (s)printf() routine can generate strings in decimal, binary, octal, or hexadecimal, using %d/e/f/g, %b, %o/t, %x/X formats respectively.<br> |
The (s)printf() routine can generate strings in decimal, binary, octal, or hexadecimal, using %d/e/f/g, %b, %o/t, %x/X formats respectively.<br> |
||
The builtin to_number() function has an inbase parameter which defaults to 10 but can be 2..16.<br> |
The builtin to_number() function has an inbase parameter which defaults to 10 but can be 2..16.<br> |
||
mpz_set_str() and mpfr_set_str() can handle input strings expressed in decimal, binary (0b prefix), or hexadecimal (0x prefix).<br> |
mpz_set_str() and mpfr_set_str() can handle input strings expressed in decimal, binary (0b prefix), or hexadecimal (0x prefix).<br> |
||
mpz_get_str() and mpfr_get_str() can generate output strings in all bases 2..62. |
mpz_get_str() and mpfr_get_str() can generate output strings in all bases 2..62. |
||
<lang Phix>?{26,0b11010,0o32,0t32,0x1A,0X1a,#1A} -- displays {26,26,26,26,26,26,26} |
<lang Phix>?{26,0b11010,0o32,0t32,0x1A,0X1a,#1A,0(16)1A} -- displays {26,26,26,26,26,26,26,26} |
||
printf(1,"%d == %b == %x\n",26) -- displays 26 == |
printf(1,"%d == 0b%b == 0x%x\n",26) -- displays 26 == 0b11010 == 0x1A |
||
?to_number("1a",{},16) -- displays 26</lang> |
?to_number("1a",{},16) -- displays 26</lang> |
||
The following |
The following routines can handle all other conversions, in bases 2 to 36. <br> |
||
Note you are expected to strip any leading "#" or "0x" from hexadecimal input strings (etc) manually, and (as-is) only use a-z not A-Z. |
Note you are expected to strip any leading "#" or "0x" from hexadecimal input strings (etc) manually, and (as-is) only use a-z not A-Z. |
||
<lang Phix>-- demo\rosetta\Convert_base.exw |
<lang Phix>-- demo\rosetta\Convert_base.exw |