-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinToBase64.fmfn
More file actions
133 lines (125 loc) · 5.2 KB
/
BinToBase64.fmfn
File metadata and controls
133 lines (125 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
------------------------------------------------------------------------------
Purpose: Converts binary text of 0's and 1's to Base64
Author: J. Michael Tritchler
Notes: This function was developed as part of importing and
exporting the contents of an ASCII text file stored
in a container field
Parameters:
text - The binary text to convert to Base64
Data Type Returned:
text
Variables:
$$currBin - stores the current six 0's and 1's from the binary
text
$$padding - stores the single of double equal signs that are
appended to the end of the base64 text to
inticate if the last character was padded with
two 0's or four 0's
$$text64 - base64 representation of the binary text
~return - Returns the base64 representation of the binary
text
Revision:
Last change: 27/09/2015 by JMT
::
27/09/2015 - JMT - Wrote original script
------------------------------------------------------------------------------
*/
Let (
[
// Grab the first six bits from the binary text
$$curBin = Left ( text ; 6 );
// If the binary text only contains 2 bits, set the padding to two
// equal signs and pad the binary text with four 0's
$$curBin = If( Length( text ) = 2; Let ( $$padding = "==" ; $$curBin & "0000" ) ; $$curBin ) ;
// If the binary text only contains 2 bits, set the padding to one
// equal sign and pad the binary text with two 0's
$$curBin = If( Length( text ) = 4; Let ( $$padding = "=" ; $$curBin & "00" ) ; $$curBin );
// Convert the current 6-bit binary number to the base64 character
// and add it to the end of the base64 text
$$text64 = $$text64 & Case (
Exact ( $$curBin ; "000000" ) ; "A";
Exact ( $$curBin ; "000001" ) ; "B";
Exact ( $$curBin ; "000010" ) ; "C";
Exact ( $$curBin ; "000011" ) ; "D";
Exact ( $$curBin ; "000100" ) ; "E";
Exact ( $$curBin ; "000101" ) ; "F";
Exact ( $$curBin ; "000110" ) ; "G";
Exact ( $$curBin ; "000111" ) ; "H";
Exact ( $$curBin ; "001000" ) ; "I";
Exact ( $$curBin ; "001001" ) ; "J";
Exact ( $$curBin ; "001010" ) ; "K";
Exact ( $$curBin ; "001011" ) ; "L";
Exact ( $$curBin ; "001100" ) ; "M";
Exact ( $$curBin ; "001101" ) ; "N";
Exact ( $$curBin ; "001110" ) ; "O";
Exact ( $$curBin ; "001111" ) ; "P";
Exact ( $$curBin ; "010000" ) ; "Q";
Exact ( $$curBin ; "010001" ) ; "R";
Exact ( $$curBin ; "010010" ) ; "S";
Exact ( $$curBin ; "010011" ) ; "T";
Exact ( $$curBin ; "010100" ) ; "U";
Exact ( $$curBin ; "010101" ) ; "V";
Exact ( $$curBin ; "010110" ) ; "W";
Exact ( $$curBin ; "010111" ) ; "X";
Exact ( $$curBin ; "011000" ) ; "Y";
Exact ( $$curBin ; "011001" ) ; "Z";
Exact ( $$curBin ; "011010" ) ; "a";
Exact ( $$curBin ; "011011" ) ; "b";
Exact ( $$curBin ; "011100" ) ; "c";
Exact ( $$curBin ; "011101" ) ; "d";
Exact ( $$curBin ; "011110" ) ; "e";
Exact ( $$curBin ; "011111" ) ; "f";
Exact ( $$curBin ; "100000" ) ; "g";
Exact ( $$curBin ; "100001" ) ; "h";
Exact ( $$curBin ; "100010" ) ; "i";
Exact ( $$curBin ; "100011" ) ; "j";
Exact ( $$curBin ; "100100" ) ; "k";
Exact ( $$curBin ; "100101" ) ; "l";
Exact ( $$curBin ; "100110" ) ; "m";
Exact ( $$curBin ; "100111" ) ; "n";
Exact ( $$curBin ; "101000" ) ; "o";
Exact ( $$curBin ; "101001" ) ; "p";
Exact ( $$curBin ; "101010" ) ; "q";
Exact ( $$curBin ; "101011" ) ; "r";
Exact ( $$curBin ; "101100" ) ; "s";
Exact ( $$curBin ; "101101" ) ; "t";
Exact ( $$curBin ; "101110" ) ; "u";
Exact ( $$curBin ; "101111" ) ; "v";
Exact ( $$curBin ; "110000" ) ; "w";
Exact ( $$curBin ; "110001" ) ; "x";
Exact ( $$curBin ; "110010" ) ; "y";
Exact ( $$curBin ; "110011" ) ; "z";
Exact ( $$curBin ; "110100" ) ; "0";
Exact ( $$curBin ; "110101" ) ; "1";
Exact ( $$curBin ; "110110" ) ; "2";
Exact ( $$curBin ; "110111" ) ; "3";
Exact ( $$curBin ; "111000" ) ; "4";
Exact ( $$curBin ; "111001" ) ; "5";
Exact ( $$curBin ; "111010" ) ; "6";
Exact ( $$curBin ; "111011" ) ; "7";
Exact ( $$curBin ; "111100" ) ; "8";
Exact ( $$curBin ; "111101" ) ; "9";
Exact ( $$curBin ; "111110" ) ; "+";
Exact ( $$curBin ; "111111" ) ; "/" );
// Save the base64 text a local variable so that the global
// variable can be cleared before exiting the function
~return = $$text64
];
If ( Length ( text ) > 6;
// Convert the next 6 bits to Base64 if not at the end
// of the binary text
BinToBase64 ( Right ( text ; Length ( text ) - 6 ) );
// Clear the global variables and return the Base64 text if
// there are no more bits to calculate
Let(
[
~return = ~return & $$padding; // Add padding if necessary
$$padding = "";
$$charCnt = "";
$$text64 = ""
];
~return
)
)
)