rendered paste bodyfunction Base64Encode( msg )
-- hZ_Debug( "### ENCODING - "..msg.." ###" );
local ret_msg = "";
local msg_length = strlen(msg);
local msgloop;
-- Make sure the incoming message is 3-byte padded (leading spaces)
if( mod(msg_length,3) == 2 ) then
msg = " "..msg;
msg_length = strlen(msg);
end
if( mod(msg_length,3) == 1 ) then
msg = " "..msg;
msg_length = strlen(msg);
end
-- Process the string
-- local b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
local b64 = "yz012LdefghSnoMNO67ABix8ETUVWXPQRCDGHIJKcpqrstuvwZab345YjklmF9.,";
for msgloop = 1,msg_length,3 do
-- hZ_Debug( "Start Loop "..msgloop );
local char1 = string.byte( msg, msgloop );
local char2 = string.byte( msg, msgloop+1 );
local char3 = string.byte( msg, msgloop+2 );
local byte1 = floor(char1 / 4); -- left 6 bits
local byte2 = (16 * mod(char1,4)) + floor(char2/16); -- right 2 bits + left 4 bits
local byte3 = (4 * mod(char2,16)) + floor(char3/64); -- right 4 bits + left 2 bits
local byte4 = mod( char3, 64 ); -- right 6 bits
local thisBlock = strsub(b64,byte1+1,byte1+1)..strsub(b64,byte2+1,byte2+1)..strsub(b64,byte3+1,byte3+1)..strsub(b64,byte4+1,byte4+1);
-- hZ_Debug( char1.." - "..char2.." - "..char3 );
-- hZ_Debug( byte1.." = "..byte2.." = "..byte3.." = "..byte4 );
-- hZ_Debug( thisBlock );
ret_msg = ret_msg..thisBlock;
end
return ret_msg
end