Тема: AHK: Конвертация из Base64 в Base10
Как правильно переконвертировать "Y7GF-5vftL" — в "448979387270691659" (инстаграмные shortcode и mediaId)? Код ниже в обе стороны конвертирует неточно.
msgbox % base64to10("Y7GF-5vftL")
; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=37465
base64to10(b64){
; Input:
; b64, a string of totalNumberOfCoefficients polynomial coefficients representing a number in base64
; Output:
; b10, the integer represented by b64 in base10.
;
; coefficient mapping:
; A -> 0
; B -> 1
; ...
; / -> 63
static coeffMap:="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
local theCurrentCoefficientInBase64, theCurrentCoefficientMappedToBase10, totalNumberOfCoefficients, b10
totalNumberOfCoefficients := strlen(b64) ; The number of coefficients in b64
b10:=0 ; The output
arrayOfTheBase64Coefficients:=strsplit(b64)
for theNumberOfTheCurrentCoefficient, theCurrentCoefficientInBase64 in arrayOfTheBase64Coefficients { ; Visit each coefficient in b64
theCurrentCoefficientMappedToBase10 := instr(coeffMap, theCurrentCoefficientInBase64, caseSensitive:=true) - 1 ; - 1 due to 1-based index of string position.
b10 += 64**(totalNumberOfCoefficients-theNumberOfTheCurrentCoefficient)*theCurrentCoefficientMappedToBase10 ; Increment the sum
}
return b10 ; Done
}
base10to64(b10) {
; Input:
; b10, an integer in base10 to be converted to a string of polynomial coefficients
; representing the same number in base64.
; Output:
; b64, a string of n polynomial coefficients, cn-1,...,c0,
; such that b10 = cn-1*64**(n-1)+cn-2*64**(n-2)...+c0*64**0
static coeffMap := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
local b64 := SubStr(coeffMap, (b10 & 63) + 1, 1)
b10 >>= 6 ; b10 >>= 6 is b10 /= 64
b10 &= 0x03FFFFFFFFFFFFFF
While (b10) {
b64 := SubStr(coeffMap, (b10 & 63) + 1, 1) . b64 ; (b10 & 63) is mod(b10,64)
b10 >>= 6 ; b10 >>= 6 is b10 /= 64
}
return b64
}