// 'where' is the digit number
function GetDigitAtPosition takes integer whatInt, integer where returns integer
local integer int = S2I(SubString(I2S(whatInt), where - 1, where))
return int
endfunction
function FindPositionOfNumber takes integer fullInt, integer searchFor returns integer
local string intString = I2S(fullInt)
local integer totalDigits = StringLength(intString)
local integer i = 0
local integer currentInt
if (searchFor>9) then
set searchFor = GetDigitAtPosition(searchFor, 0)
endif
loop
set currentInt = GetDigitAtPosition(fullInt, i)
exitwhen currentInt==searchFor
set i= i+1
exitwhen i==totalDigits
endloop
if (currentInt==searchFor) then
return i
endif
return -1
endfunction
function BaseConversion takes string input, integer inputBase, integer outputBase returns string
local string charMap = "0123456789abcdefghijklmnopqrstuvwxyz"
local string s
local string result = ""
local integer val = 0
local integer i
local integer p = 0
local integer pow = 1
local string sign = ""
if ( inputBase < 2 or inputBase > StringLength(charMap) or outputBase < 2 or outputBase > StringLength(charMap) ) then
// Bases are invalid or out of bounds
return "Invalid bases given"
endif
if ( SubString(input, 0, 1) == "-" ) then
set sign = "-"
set input = SubString(input, 1, StringLength(input))
endif
set i = StringLength(input)
// Get the integer value of input
set input = StringCase(input, false)
loop
exitwhen i <= 0
set s = SubString(input, i-1, i)
set p = 0
loop
if ( p >= inputBase ) then
// Input cannot match base
return "Input does not match base!"
endif
if ( s == SubString(charMap, p, p+1) ) then
set val = val + pow*p
set pow = pow * inputBase
exitwhen true
endif
set p = p + 1
endloop
set i = i - 1
endloop
loop
set p = ModuloInteger(val, outputBase)
set result = SubString(charMap, p, p+1) + result
set val = val / outputBase
exitwhen val <= 0
endloop
return sign + result
endfunction
// Returns -1 if find was not found anywhere
function StringFind takes string find, string subject, integer offset returns integer
local integer len = StringLength(find)
local integer pos = offset
local string s
local string str
if ( offset < 1 ) then
set pos = 1
endif
if ( find == "" ) then
return -1
endif
loop
set s = SubString(subject, pos, pos+len-1)
if ( s == find ) then
return pos
endif
if ( SubString(subject, pos, pos) == "" ) then
return -1
endif
set pos = pos + 1
endloop
return -1
endfunction
function StringReplace takes string find, string replace, string subject returns string
local integer pos = 1
local integer i
local string result = ""
local integer len = StringLength(find)
loop
set i = StringFind(find, subject, pos)
if ( i > -1 ) then
if ( i > pos ) then
set result = result + SubString(subject, pos, i-1) + replace
else
set result = result + replace
endif
set pos = i + len
else
set result = result + SubString(subject, pos, 999999)
return result
endif
endloop
return subject
endfunction
function GetDataChunk takes integer fullInt returns real
local integer endAt = FindPositionOfNumber(fullInt,9)
local string workWith = SubString(I2S(fullInt), 0, endAt)
set workWith = StringReplace("8",".",workWith)
set workWith = StringReplace("7","-",workWith)
set workWith = BaseConversion(workWith, 7, 10)
return S2R(workWith)
endfunction