Decimal to Base64 conversion + Custom character list

Get help with using AutoHotkey and its commands and hotkeys
User avatar
dmg
Posts: 239
Joined: 02 Oct 2013, 01:43
Location: "Twelve days north of Hopeless and a few degrees south of Freezing to Death"
Contact:

Decimal to Base64 conversion + Custom character list

25 Sep 2017, 03:28

Greetings!

I am looking for code that does nothing but give the Base64 equivalent of any decimal integer fed to it, and code to do the reverse. I also would very much like the Base64 character list to be customizable, and do all this in-language, with no dllcalls or similar. Obviously I could do this using hand coded conversion tables and stringreplace, but that would be stupid. :wtf:

I found this code on the archived forum:
https://autohotkey.com/board/topic/5545 ... erdecoder/

Sadly, I can't make head nor tail of the code. I tried tinkering with it to get to the core elements but no joy. And the explanation on that page may as well be in Martian.

Any help will be greatly appreciated. :morebeard:
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
User avatar
dmg
Posts: 239
Joined: 02 Oct 2013, 01:43
Location: "Twelve days north of Hopeless and a few degrees south of Freezing to Death"
Contact:

Re: Decimal to Base64 conversion + Custom character list

26 Sep 2017, 03:10

It has been 24 hours, and this is about to be pushed to the third page, so... Bump! :wave:
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
User avatar
jNizM
Posts: 2295
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Decimal to Base64 conversion + Custom character list

26 Sep 2017, 05:28

So it is just a string to base64 enocder?
If yes -> base64.ahk (via DllCall)
Alternative -> b64Decode (without DllCall - but did just the decode translation so far - and you need to change a bit)
[AHK] 1.1.26.01 x64 Unicode | [WIN] 10 Pro (Version 1703) x64
My GitHub Profile | Donations are appreciated if I could help you
User avatar
dmg
Posts: 239
Joined: 02 Oct 2013, 01:43
Location: "Twelve days north of Hopeless and a few degrees south of Freezing to Death"
Contact:

Re: Decimal to Base64 conversion + Custom character list

26 Sep 2017, 06:50

Greetings, jNizM! And thank you for responding.

What I am looking for is a number converter. I only need it to convert from decimal to Base64 and back again. And I need to be able to understand the code in order to use and modify it. The link I posted seems closest to what I need, but I CANNOT understand that code. I appreciate the links you provided, but I am afraid neither will work for me as is, and I certainly have no idea how to change them. :?

Would it be possible to help me understand how I might change any of the afore mentioned code to meet my criteria?

Criteria:
1. Convert decimal integers to Base64 and back again.
2. Customizable character table for the Base64 representation.
3. Done entirely in AutoHotkey language, with no DLLCalls or similar.
4. Final code must be readable, meaning vars have actual names instead of single characters, and a certain degree of code comments and documentation be present. :wtf:

Does this sound possible? Thank you again for responding. 8-)
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
User avatar
dmg
Posts: 239
Joined: 02 Oct 2013, 01:43
Location: "Twelve days north of Hopeless and a few degrees south of Freezing to Death"
Contact:

Re: Decimal to Base64 conversion + Custom character list

27 Sep 2017, 11:46

... Bump?
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
User avatar
dmg
Posts: 239
Joined: 02 Oct 2013, 01:43
Location: "Twelve days north of Hopeless and a few degrees south of Freezing to Death"
Contact:

Re: Decimal to Base64 conversion + Custom character list

28 Sep 2017, 18:36

While waiting for replies I continued to work on the code. I have managed to work out enough of the Dec > B64 portion to re write it:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


I can't say I understand the math involved here but this code appears to work, and can be expanded to handle VERY large numbers.

Now, I have so far failed to adapt the B64 > Dec code to work with my function. Any and all help to do this will be greatly appreciated.

Goal:
Convert the Base64 to Decimal code from my original post so it is compatible with the Decimal to Base64 code in this post. It will need to accept a character string (Base64 integer) and output the equivalent integer in Decimal format.
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
just me
Posts: 4862
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Decimal to Base64 conversion + Custom character list

29 Sep 2017, 10:11

Hi dmg,

I don't get what you are trying to achieve. Base64 is designed to encode a sequence of bytes / characters in the range of 0 to 255. If you compare your result with the results of an online encoder or the linked script, you should see that it has nothing to do with Base64.
Remaining with AHK 1.1.25.02 until v2 will become beta.
User avatar
dmg
Posts: 239
Joined: 02 Oct 2013, 01:43
Location: "Twelve days north of Hopeless and a few degrees south of Freezing to Death"
Contact:

Re: Decimal to Base64 conversion + Custom character list

29 Sep 2017, 16:00

Greetings just me! thank you for responding.

I am afraid that isn't quite accurate... Are you maybe thinking of 8 bit binary, where 8 bits/numeric positions represent 256 possible integers (0-255)?

Binary is also known as Base2, because each position can hold two possible digits (0 or 1). Most western cultures use the Decimal system (aka Base10) which uses 10 digits (0-9). Another common one is Base16, aka Hexadecimal, which uses 16 characters (0-9 and A-F) to represent 16 possible values per number column. Base64 works exactly the same way, but uses 64 characters.

Please see these links:
https://simple.wikipedia.org/wiki/Base_(mathematics)
https://www.youtube.com/watch?v=gocwRvLhDf8
http://convertxy.com/index.php/numberbases/

It is true that Base64 is used as part of an encoding system to convert data to a character set with broad compatibility, but that is not what Base64 IS in of itself.

What I want is two functions. One to convert decimal to Base64 and one to convert back again. I have the first, now I need the second. :D
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
Helgef
Posts: 2472
Joined: 17 Jul 2016, 01:02
Contact:

Re: Decimal to Base64 conversion + Custom character list

29 Sep 2017, 17:16

I need the second

Code: [Select all] [Download] GeSHi © Codebox Plus

f(x){
static chars:="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
local k,v,p,y
p:=strlen(x),y:=0
for k, v in strsplit(x)
y+= 64**(p-k)*(instr(chars,v,true)-1)
return y
}

:morebeard:
User avatar
dmg
Posts: 239
Joined: 02 Oct 2013, 01:43
Location: "Twelve days north of Hopeless and a few degrees south of Freezing to Death"
Contact:

Re: Decimal to Base64 conversion + Custom character list

30 Sep 2017, 07:52

Thank you Helgef! This does indeed appear to do what I wanted. :dance:

Would you be kind, and patient enough to add some information on how the code works? In code comments or just a paragraph on what the elements are doing? Even changing the var names to words relevant to what the vars do would help a lot in my being able to understand it.

Thank you again! :bravo:
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
User avatar
dmg
Posts: 239
Joined: 02 Oct 2013, 01:43
Location: "Twelve days north of Hopeless and a few degrees south of Freezing to Death"
Contact:

Re: Decimal to Base64 conversion + Custom character list

30 Sep 2017, 07:56

While I am already making a nuisance of myself, could you have a look at the Decimal to Base64 function I posted and see if it can be improved? In particular, at the moment it needs the max number of Base64 number positions to be set in the code, and has to use Trim to remove padding before output. Can these be fixed? :cookie: :cookie: :cookie:
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
Helgef
Posts: 2472
Joined: 17 Jul 2016, 01:02
Contact:

Re: Decimal to Base64 conversion + Custom character list

30 Sep 2017, 18:48

Hello. The algorithm for your base64 function is fine, the implementation can be tweaked a little, see base10to64
Even changing the var names to words relevant to what the vars do would help a lot

What could be more clear than k,v,p,y,x? :lol:
I have added some comments,

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Cheers. :wave:
just me
Posts: 4862
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Decimal to Base64 conversion + Custom character list

01 Oct 2017, 06:03

Hi Helgef,

base10to64:
I had almost the same idea. Don't pass negative integers to the function. ;)
Remaining with AHK 1.1.25.02 until v2 will become beta.
Helgef
Posts: 2472
Joined: 17 Jul 2016, 01:02
Contact:

Re: Decimal to Base64 conversion + Custom character list

01 Oct 2017, 06:16

:thumbup: Maybe something like this then,

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

just me
Posts: 4862
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Decimal to Base64 conversion + Custom character list

01 Oct 2017, 07:33

This seems to work with your original version of base64to10:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Edit: changed 0x0FFFFFFFFFFFFFFF to 0x03FFFFFFFFFFFFFF
Remaining with AHK 1.1.25.02 until v2 will become beta.
User avatar
dmg
Posts: 239
Joined: 02 Oct 2013, 01:43
Location: "Twelve days north of Hopeless and a few degrees south of Freezing to Death"
Contact:

Re: Decimal to Base64 conversion + Custom character list

01 Oct 2017, 09:00

Ummm... Not to seem ungrateful, but if the last few posts were intended as answers to my posts, then I regret to inform you I find them next to incomprehensible. NAME YOUR VARIABLES AND EXPLAIN YOUR CODE, DARN YOU ALL!

Sorry for the outburst. It is very frustrating to ask questions only to get well meaning, but not entirely helpful answers. It's just... When one is trying to learn something, but the available instruction is well over one's head, it makes the learning more difficult than one wishes it would be.

I really do appreciate everyone taking time to respond at all. Sincerely. :? :? :? :? :? :? :? :?

Edit:
Just saying, if I can't understand this y+= 64**(p-k)*(instr(chars,v,true)-1) as code, then why would cn-1*64**(n-1)+cn-2*64**(n-2)...+c0*64**0 as a comment make it any clearer? :wtf:
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
Helgef
Posts: 2472
Joined: 17 Jul 2016, 01:02
Contact:

Re: Decimal to Base64 conversion + Custom character list

01 Oct 2017, 10:00

@ just me, seems ok :thumbup:
dmg wrote:if I can't understand this y+= 64**(p-k)*(instr(chars,v,true)-1) as code, then why would cn-1*64**(n-1)+cn-2*64**(n-2)...+c0*64**0 as a comment make it any clearer?

Because now the variables meaning are defined (named) in the comments. I'm not sure how to make it clearer other than improving the textual formatting, I can type it up for you in LaTeX. Making longDescriptiveVariableNames in code will not make it clearer imo, I try,

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


More specific questions are also welcome.
Cheers.
User avatar
dmg
Posts: 239
Joined: 02 Oct 2013, 01:43
Location: "Twelve days north of Hopeless and a few degrees south of Freezing to Death"
Contact:

Re: Decimal to Base64 conversion + Custom character list

01 Oct 2017, 10:17

Thank you Helgef. Again, I am sorry for the outburst. I will study all the code posted so far and post back in a few days. I really do appreciate it. :)
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
Helgef
Posts: 2472
Joined: 17 Jul 2016, 01:02
Contact:

Re: Decimal to Base64 conversion + Custom character list

02 Oct 2017, 04:13

dmg wrote:Thank you Helgef. Again, I am sorry for the outburst. I will study all the code posted so far and post back in a few days. I really do appreciate it. :)

No problems dmg :wave:
Here is a more readable version of the comments defining the B10 - B64 relation,
b64.png
b64.png (2.38 KiB) Viewed 339 times

b10.png
(6.28 KiB) Not downloaded yet

Image credits: latex2png.com

I recommend you try to write a bM:=baseNtoM(BN,N,M) function, it will benefit your understanding.

Cheers.
User avatar
jeeswg
Posts: 2959
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Decimal to Base64 conversion + Custom character list

07 Oct 2017, 14:03

I haven't tried anybody else's scripts here, but I would ask, what magnitude of number can your functions handle?

This question has prompted me to attempt some 'big int' functions that I had been meaning to write for a long time, i.e. functions than can handle numbers of infinite magnitude, by using string manipulation. Currently the functions can only handle positive integers, and can't handle subtraction or division, but all the difficult bits have been done. They have not been thoroughly tested.

I'm not sure what the best big int library is for AHK, but I know that this is good:
Scientific Maths - infinite precision Mathematics Library - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/93516-scientific-maths-infinite-precision-mathematics-library/
Scientific Maths - Math Lib for Unlimited precision - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=465

@dmg: Hopefully everything is reasonably easy to follow here.

Functions so far:
JEE_BigIntAdd(oArray*) ;a sum function
JEE_BigIntMult(oArray*) ;a product function
JEE_BigIntPow(vNum, vPower)
JEE_BigIntCompare(vNum1, vNum2)
JEE_BigIntBaseToDec(vNumX, vBase, vList:="")
JEE_BigIntDecToBase(vNum, vBase, vList:="")

Btw re. base64, the encoding for storing file data, starts with ABC ... rather than 0123 ..., which is a little unusual if you're storing actual numbers.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Btw I would welcome any stand-alone functions specifically optimised for dec to/from base 64, that can/can't handle infinite magnitude. Also, dmg is right, the scripts posted so far, could be better explained, I would welcome any clarifications.

Some algorithm details:
Spoiler

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Capn Odin, Google [Bot], Niejaki, Odlanir and 45 guests