Project

General

Profile

$base » History » Version 9

Paul Janson, 12/13/2019 12:11 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 9 Paul Janson
N - The number to convert. Values above 2^53 are (AdiIRC only)
12
inbase - The base to convert from. (2-64) 37-64 are (AdiIRC only)
13
outbase - The base to convert to. (2-64) 37-64 are (AdiIRC only)
14
zeropad - If length is higher than zeropad, pad numbers with zeroes to the specified zeropad length. Values above 100 are (AdiIRC only) 
15
precision - Truncate fractions/decimal places to the specified precision. (Can't use if Zeropad is blank) Values above 6 are (AdiIRC only)
16 1 Per Amundsen
17 8 Paul Janson
base: From 2-36, base N uses the first N characters from the alphanumeric alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
18
From 37-64, base N uses the first N characters from the 64-char mime alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
19 1 Per Amundsen
20 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.
21
22
*Examples*
23
24 1 Per Amundsen
<pre>
25 5 Per Amundsen
; Returns F
26 1 Per Amundsen
//echo -ag $base(15,10,16)
27
28 5 Per Amundsen
; Returns 1.8
29 1 Per Amundsen
//echo -ag base(1.5,10,16)
30
31
; Returns 002
32
//echo -ag $base(2,10,16,3)
33 8 Paul Janson
34
; Returns fraction chopped to 4 digits
35
//echo -a $base($pi,10,10,0,4)
36
37
; Zero-Pad for base 37-64 uses "A", while base 2-36 uses '0'
38
//echo -a $base(deadbeef,16,64,20)
39
//echo -a $base(deadbeef,16,10,20)
40
41
; Using same in/out base to ensure number contains at least 12 digits:
42
//echo -a $base($ticks,10,10,12)
43
44
; Accurate results above the 2^53 limit:
45
//var %a $sha256(abc) | echo -a $lower($base($base(%a,16,10),10,16)) same as | echo -a %a
46
47
; Base 37-64 use case-sensitive alphabets
48
//echo -a $base(deadbeef,64,10) vs $base(DEADBEEF,64,10)
49
50
; Only if inbase is 16, optional '0x' prefix does not affect output
51
//echo -a $base(0xdeadbeef,16,10) same as $base(deadbeef,16,10)
52
53
; Allows 2-35 using invalid base36 characters, or 37-63 using invalid base64 chars
54
//echo -a $base(MZ,10,10) is $base(M,36,10) * 10 + $base(Z,10,10) = 255
55
56
; 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.
57
//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)
58 1 Per Amundsen
</pre>