Page 2 of 2

Re: Decimal to Base64 conversion + Custom character list

Posted: 15 Oct 2017, 08:26
by dmg
@Helgef

I have studied your functions, and I think I see how they work, more or less. Some of the math is well over my head but I was able to modify the code and have it still function. This version should allow use of any desired character table, the length of which determines the 'Base', with the possible exception of Base2 (binary) simply because of the need for a consistent 8 bit length. Please have a look and see if there are any obvious flaws. You should be able to use this site to check results, assuming you use a standard character table: http://extraconversion.com/base-number :ugeek:

Code: Select all

#noenv
#singleinstance, ignore
setbatchlines, 10ms
setworkingdir, %a_scriptdir%

table := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
input := 64

msgbox, % "Table: " . table . "`n`nBase10: " . input . "`nBase" . strlen(table) . ": " base10to64(table, input) . "`nBase10: " .  base64to10(table, base10to64(table, input))


base10to64(table, input)
 {
   base := strlen(table)
   loop
    {
      output := SubStr(table, mod(input, base) + 1, 1) . output
      input /= base
    } until !input

   return output 
 }


base64to10(table, input)
 {
   base := strlen(table)
   in_length := strlen(input)
   output := 0
   for position, character in strsplit(input)
    {
      character := instr(table, character, 1) - 1
      output += base ** (in_length - position) * character
    }
   return output
 }
I do have a math question. Why is X & Y equivalent to X mod (Y + 1)? Please keep in mind my math skills are roughly those of a 5 year old. :crazy:


@jeeswg

Wow. Those functions are interesting! I can't understand most of the math in the code, but the idea of handling math as strings to get around the 64 bit integer limit is amazing. :o

I played around with prime number code a while back, but I lost interest because of the limit on the size of the primes it could work with. No idea if your functions could be applied...

Re: Decimal to Base64 conversion + Custom character list

Posted: 15 Oct 2017, 11:51
by Helgef
My first impression is that it seems fine dmg :thumbup: . I might take a closer look later if I get the time, and remember. :P
I'd probably let base be a paramter rather than strlen(table) though. You should be able to easily write BM:=baseNtoM(BN,N,M) from what you have now.

@jeeswg, :clap:

Cheers.

Re: Decimal to Base64 conversion + Custom character list

Posted: 15 Oct 2017, 19:28
by dmg
I know it would be better coding practice to clearly define the base, but since the base will always be the same as the number of characters in the conversion table it seemed unnecessary to define them separately.

I am afraid I have no idea what "You should be able to easily write BM:=baseNtoM(BN,N,M) from what you have now." means... :eh: :?:

Re: Decimal to Base64 conversion + Custom character list

Posted: 16 Oct 2017, 01:49
by Helgef
BM:=baseNtoM(BN,N,M) would take a number (string) BN in base N and return the corresponding number (string) BM in base M. The bases N and M should be able to be any number between 2 and the lenght of the table.

Re: Decimal to Base64 conversion + Custom character list

Posted: 16 Oct 2017, 03:53
by dmg
That is what these functions now do:

Code: Select all

table1 := "01"
table2 := "0123456789ab"
table3 := "0123456789abcdef"
table4 := "0123456789abcdefghijklmnopqrstuv"
table5 := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

input := 64

loop, 5
 {
   msgbox, % "Table" . a_index . ": " . table . "`n`nBase10: " . input . "`nBase" . strlen(table%a_index%) . ": " base10to64(table%a_index%, input) . "`nBase10: " .  base64to10(table%a_index%, base10to64(table%a_index%, input))
 }



base10to64(table, input)
 {
   base := strlen(table)
   loop
    {
      output := SubStr(table, mod(input, base) + 1, 1) . output
      input /= base
    } until !input

   return output 
 }


base64to10(table, input)
 {
   base := strlen(table)
   in_length := strlen(input)
   output := 0
   for position, character in strsplit(input)
    {
      character := instr(table, character, 1) - 1
      output += base ** (in_length - position) * character
    }
   return output
 }
These functions will operate on any designated base. Just feed it the number in Base10 along with the desired conversion table. Try it. :D

EDIT: or do you mean you want it to accept any base and convert to any other base? That would be very difficult, if only because you would have to code so many different conversion tables. But I suppose you can feed it the table for the input as well as the output... :think:

Re: Decimal to Base64 conversion + Custom character list

Posted: 16 Oct 2017, 15:27
by Helgef
Hello :wave:
I meant that it could be organised nicer, like this,

Code: Select all

baseNtoM(BN,tableN,tableM){
	return base10to64(tableM, base64to10(tableN,BN))
}
; Example
table2 := "01"
table10 := "0123456789"

table16 := "0123456789abcdef"
B16 := "ff"

msgbox % b2:=baseNtoM(B16,table16, table2)
msgbox % b16:=baseNtoM(b2,table2, table16)
msgbox % b10:=baseNtoM(B2,table2, table10)
msgbox % b16:=baseNtoM(B10,table10, table16)
where 64 in base10to64 and base64to10 should be changed to, eg, N.
Cheers.

Edit
EDIT: or do you mean you want it to accept any base and convert to any other base?
I missed your edit, I had the tab open and didn't refresh. Yes, I mean that, and you see, it is very easy.