on decipherAbookmark(n)
set my theChars to {} & the id of (text 123 thru -1 of n)
set my newChars to {}
set myCount to 0
set theResult to 0
set tooLow to false
set firstSum to 0
repeat with theid in my theChars
if theid > 96 then
set theNum to theid - 87
else if theid > 64 then
set theNum to theid - 55
else
set theNum to theid - 48
end if
if theNum ≥ 0 and theNum ≤ 16 then set theResult to 16 * theResult + theNum
set myCount to myCount + 1
if (myCount = 2) then
if theResult < 129 or theResult > 192 then
if firstSum ≠ 0 then
copy firstSum to the end of my newChars
set firstSum to 0
end if
end if
if theResult < 31 then
if not tooLow then
copy 47 to the end of my newChars
set tooLow to true
end if
else
set tooLow to false
# log "Test: " & theResult
if theResult < 129 then
copy theResult to the end of my newChars
else if theResult < 192 then
set firstSum to firstSum * 64 + (theResult - 128)
else if theResult < 224 then
set firstSum to firstSum * 64 + (theResult - 192)
else if theResult < 240 then
set firstSum to firstSum * 64 + (theResult - 224)
else if theResult < 245 then
set firstSum to firstSum * 64 + (theResult - 240)
end if
end if
if my newChars ends with {46, 97, 112, 112} then return character id (my newChars) #exit repeat
set theResult to 0
set myCount to 0
end if
end repeat
end decipherAbookmark