Project

General

Profile

$base » History » Version 10

Per Amundsen, 02/16/2023 09:09 PM

1 1 Per Amundsen
_Added in 1.9.0_
2
3
*$base(N,inbase,outbase,zeropad,precision)*
4
5
Converts number N from inbase to outbase. The last two parameters are optional.
6
7 6 Per Amundsen
_In version 3.3+, it should work reliably to at least base64, otherwise to base32._
8
9 1 Per Amundsen
*Parameters*
10
11 10 Per Amundsen
table(ktable).
12
|*Parameter*|*Description*|
13
| N | The number to convert. Values above 2^53 are (AdiIRC only) |
14
| inbase | The base to convert from. (2-64) 37-64 are (AdiIRC only) |
15
| outbase | The base to convert to. (2-64) 37-64 are (AdiIRC only) |
16
| zeropad | If length is higher than zeropad, pad numbers with zeroes to the specified zeropad length. Values above 100 are (AdiIRC only) |
17
| precision | Truncate fractions/decimal places to the specified precision. (Can't use if Zeropad is blank) Values above 6 are (AdiIRC only) |
18 1 Per Amundsen
19 8 Paul Janson
base: From 2-36, base N uses the first N characters from the alphanumeric alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
20
From 37-64, base N uses the first N characters from the 64-char mime alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
21 1 Per Amundsen
22 8 Paul Janson
Notes: base 2-36 are case-insensitive, but base 37-64 are case-sensitive. This base 32 uses the first 32 characters of the base36 alphabet, which is different than the 'a' switch in $encode which uses A-Z and 2-7. Even though this base 64 uses the same alphabet as $encode's mime 'm' switch, this is NOT the same as mime-encoding. It does support the '=' padding used by mime, and returns results compatible with mime ONLY if the base64 string's length is a multiple of 4, or the base64 string used by $base must be left-padded with "A" into becoming a length that's a multiple of 4 compatible with the mime encoding of a string whose length is a multiple of 3. zero-pad means inserting the lowest character of that base's 'alphabet', which for base 37-64 is the "A" character.
23
24
*Examples*
25
26 1 Per Amundsen
<pre>
27 5 Per Amundsen
; Returns F
28 1 Per Amundsen
//echo -ag $base(15,10,16)
29
30 5 Per Amundsen
; Returns 1.8
31 1 Per Amundsen
//echo -ag base(1.5,10,16)
32
33
; Returns 002
34
//echo -ag $base(2,10,16,3)
35 8 Paul Janson
36
; Returns fraction chopped to 4 digits
37
//echo -a $base($pi,10,10,0,4)
38
39
; Zero-Pad for base 37-64 uses "A", while base 2-36 uses '0'
40
//echo -a $base(deadbeef,16,64,20)
41
//echo -a $base(deadbeef,16,10,20)
42
43
; Using same in/out base to ensure number contains at least 12 digits:
44
//echo -a $base($ticks,10,10,12)
45
46
; Accurate results above the 2^53 limit:
47
//var %a $sha256(abc) | echo -a $lower($base($base(%a,16,10),10,16)) same as | echo -a %a
48
49
; Base 37-64 use case-sensitive alphabets
50
//echo -a $base(deadbeef,64,10) vs $base(DEADBEEF,64,10)
51
52
; Only if inbase is 16, optional '0x' prefix does not affect output
53
//echo -a $base(0xdeadbeef,16,10) same as $base(deadbeef,16,10)
54
55
; Allows 2-35 using invalid base36 characters, or 37-63 using invalid base64 chars
56
//echo -a $base(MZ,10,10) is $base(M,36,10) * 10 + $base(Z,10,10) = 255
57
58
; Demonstrates how 64 in $base is compatible with 'm' mime in $encode only if mime string's length is a multiple of 4. If edit the ++adiirc to be a mime string that's not a multiple of 4, the output no longer matches.
59
//var -s %mime ++adiirc , %base64 %mime | ;while ($calc($len(%base64) % 4)) var -s %base64 A $+ %base64 | bset -t &v 1 %mime | noop $decode(&v,bm) $encode(&v,bx) | echo 4 -a $bvar(&v,1-).text | echo 3 -a $base(%base64,64,16)
60 1 Per Amundsen
</pre>