IanN1990 Posted April 14, 2017 Share Posted April 14, 2017 (edited) Hi All, After hours of searching I couldn’t find a autoit base to base converter. So I wrote one. It’s not very fast and only practical in exact situations (ie what I wrote it for) but am sharing it anyways encase it helps someone. expandcollapse popup#NoTrayIcon #include <string.au3> #include <array.au3> #include <Crypt.au3> Local $Base16 = Array("0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F") Local $Base16Binary = Array("0000,0001,0010,0011,0100,0101,0110,0111,1000,1001,1010,1011,1100,1101,1110,1111") Local $Base64 = Array("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9,!,+") Local $Base64Binary = Array("000000,000001,000010,000011,000100,000101,000110,000111,001000,001001,001010,001011,001100,001101,001110,001111,010000,010001,010010,010011,010100,010101,010110,010111,011000,011001,011010,011011,011100,011101,011110,011111,100000,100001,100010,100011,100100,100101,100110,100111,101000,101001,101010,101011,101100,101101,101110,101111,110000,110001,110010,110011,110100,110101,110110,110111,111000,111001,111010,111011,111100,111101,111110,111111") $Randomhex = RandomHex("9") ;This Hex is 18 chars, each 4 bits. (72 bits total). Base64 are 6 bits a char. 72/6 = 12 ConsoleWrite($Randomhex & @crlf) $Base64Example = Base($Randomhex, 16) ConsoleWrite($Base64Example & @CRLF) $Base16Example = Base($Base64Example, 64) ConsoleWrite($Base16Example & @CRLF) $Randomhex = RandomHex("16") ;This hex is 32 chars, each 4 bits. (128 bits total). Base64 are 6 bits a char. 128/6 = 21.33 (a float) so a padding F is added. 132/6 = 22 ConsoleWrite($Randomhex & @crlf) $Base64Example = Base($Randomhex, 16) ConsoleWrite($Base64Example & @CRLF) $Base16Example = Base($Base64Example, 64) ConsoleWrite($Base16Example & @CRLF) Func Base($String, $BaseInOut) If $BaseInOut = 16 Then If StringLen($String) = 128 Then $String &= "F" $Binary = _ConvertBinary($String, $Base16, $Base16Binary) Return _ConvertBase($Binary, $Base64, $Base64Binary, 6) ElseIf $BaseInOut = 64 Then $Binary = _ConvertBinary($String, $Base64, $Base64Binary) $Base = _ConvertBase($Binary, $Base16, $Base16Binary, 4) If StringLen($Base) = 129 Then $Base = StringTrimRight($Base, 1) EndIf Return $Base EndFunc Func _ConvertBinary($String, $BaseIn, $BaseInBinary) Dim $Binary, $Stringsplit = StringSplit($String, "") For $I = 1 to ubound($Stringsplit)-1 $Value = _ArraySearch($BaseIn, $Stringsplit[$i], 0, 0, 1, 2) $Binary &= $BaseInBinary[$Value] Next Return $Binary EndFunc Func _ConvertBase($Binary, $BaseOut, $BaseOutBinary, $Amount) Dim $I=1, $Base Do $Value = _ArraySearch($BaseOutBinary, StringMid($Binary, $i, $Amount), 0, 0, 1, 2) If $Value <> -1 Then $Base &= $BaseOut[$Value] $I += $Amount Until $Value = -1 Return $Base EndFunc Func Array($Test) Return StringSplit($Test, ",") EndFunc Func RandomHex($Amount) Local $tBuff = DllStructCreate("byte[" & $Amount & "]") _Crypt_GenRandom($tBuff, DllStructGetSize($tBuff)) Return StringTrimLeft(DllStructGetData($tBuff, 1), 2) EndFunc Small note as the example shows. Inperfect conversations need hardcording Edited April 14, 2017 by IanN1990 Link to comment Share on other sites More sharing options...
IanN1990 Posted April 15, 2017 Author Share Posted April 15, 2017 (edited) Though the code above is functional the speed (19ms per conversation) is just too slow for my project as it is excuted almost 3 billion times. Below is a new version that only takes 7ms per conversation (170% faster). Enjoy expandcollapse popup#NoTrayIcon #include <Crypt.au3> Local $ArrayBase16 = Array("0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F") Local $ArrayBase16Binary = Array("0000,0001,0010,0011,0100,0101,0110,0111,1000,1001,"","","","","","","",1010,1011,1100,1101,1110,1111") Local $ArrayBase64 = Array("A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9,=,?") Local $ArrayBase64Binary = Array("110100,110101,110110,110111,111000,111001,111010,111011,111100,111101,"", "", "",111110,"",111111,"",000000,000001,000010,000011,000100,000101,000110,000111,001000,001001,001010,001011,001100,001101,001110,001111,010000,010001,010010,010011,010100,010101,010110,010111,011000,011001,"","","","","","",011010,011011,011100,011101,011110,011111,100000,100001,100010,100011,100100,100101,100110,100111,101000,101001,101010,101011,101100,101101,101110,101111,110000,110001,110010,110011") $Hex = RandomHex(9) ConsoleWrite($Hex & @crlf) $Bas64 = Base($Hex, 64) ConsoleWrite($Bas64 & @crlf) $Bas16 = Base($Bas64, 16) ConsoleWrite($Bas16 & @crlf) Func Base($String, $BaseInOut) If $BaseInOut = 64 Then If StringLen($String) = 32 Then $String &= "F" $Binary = _ConvertBinary($String, $ArrayBase16Binary) $Base = _ConvertBase($Binary, $ArrayBase64, 6) ElseIf $BaseInOut = 16 Then $Binary = _ConvertBinary($String, $ArrayBase64Binary) $Base = _ConvertBase($Binary, $ArrayBase16, 4) If StringLen($Base) = 33 Then $Base = StringTrimRight($Base, 1) EndIf Return $Base EndFunc Func _ConvertBinary($String, $BaseInBinary) Dim $Binary3, $Stringsplit = StringToASCIIArray($String) For $I = 0 to ubound($Stringsplit)-1 $Binary3 &= $BaseInBinary[$Stringsplit[$i]-47] Next Return $Binary3 EndFunc Func _ConvertBase($Binary, $BaseOut, $Amount) Dim $Return1, $BinarySplit = StringSplit($Binary, "") If $Amount = 6 Then For $i = 1 to ubound($BinarySplit)-1 Step 6 $Char = $BinarySplit[$i]*32 + $BinarySplit[$i+1]*16 + $BinarySplit[$i+2]*8 + $BinarySplit[$i+3]*4 + $BinarySplit[$i+4]*2 + $BinarySplit[$i+5]*1 $Return1 &= $BaseOut[$Char+1] Next Else For $i = 1 to ubound($BinarySplit)-1 Step 4 $Char = $BinarySplit[$i]*8 + $BinarySplit[$i+1]*4 + $BinarySplit[$i+2]*2 + $BinarySplit[$i+3]*1 $Return1 &= $BaseOut[$Char+1] Next EndIf Return $Return1 EndFunc Func Array($Test) Return StringSplit($Test, ",") EndFunc Func RandomHex($Amount) Local $tBuff = DllStructCreate("byte[" & $Amount & "]") _Crypt_GenRandom($tBuff, DllStructGetSize($tBuff)) Return StringTrimLeft(DllStructGetData($tBuff, 1), 2) EndFunc Edited April 15, 2017 by IanN1990 Link to comment Share on other sites More sharing options...
jchd Posted April 16, 2017 Share Posted April 16, 2017 Did you try Ward base64 machine code version? (Jump at the end of the thread.) This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
IanN1990 Posted April 16, 2017 Author Share Posted April 16, 2017 (edited) I did but found it doesn't encode how i was expecting it 46FCE66694E66D1712 = Base16 = 18 chars Using Wards Base64 Machine code the result is NDZGQ0U2NjY5NEU2NkQxNzEy = Base64 = 25 chars (40% larger) Using My Base64 the result is RvzmZpTmbRcS = Base64 = 12 chars (35% smaller) The main reason for encoding is to the reduce the ASCII key length to be stored or transmitted. Also using a third-party converter - http://tomeko.net/online_tools/hex_to_base64.php?lang=en gives the same results to mine. Edited April 16, 2017 by IanN1990 Link to comment Share on other sites More sharing options...
jchd Posted April 16, 2017 Share Posted April 16, 2017 You're clearly confusing the string "46FCE66694E66D1712" and the binary value Binary("0x46FCE66694E66D1712"). Try by yourself: #Include "Base64.au3" Local $Data = Binary("0x46FCE66694E66D1712") ;~ Local $Data = BinaryToString(StringToBinary("The quick brown fox jumps over the lazy ɖɘɠɥლოჸᴟ⁈ℕℤℚℝℂℍℙ∑∀∋≢≰⋟⋞⟪⟫ dog", 4), 1) Local $Encode = _Base64Encode($Data) MsgBox(0, 'Base64 Encode Data', $Encode) Local $Decode = _Base64Decode($Encode) MsgBox(0, 'Base64 Decode Data (Binary Format)', $Decode) IanN1990 1 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now