Hello to all,
Am facing this task of converting to a word var, for use in some calculations, a value that is received as a ascii string, ex: "00800" that need it as a value of 800 in a word var.
Came up with this code below, but need some advice on a more efficient method to get same result.
Any ideas would be great.
function AsciiStringToWord(byte IN str[]) return word is
-- Max LEN in str is 5 chars (Converting to word: max= 65535)
var word wRtn
var word len = count(str)
var byte i
var byte ii
--
for len using i loop -- Can be use STEP -1 ??
--wRtn = wRtn + str[i]*(10^i) -- Can be used "^" for power operations ???
-- This code would be enough
-- Here my alternate code for same result
case i of
0: wRtn=wRtn+(str[5 - len + i]-48)
1: wRtn=wRtn+(str[5 - len + i]-48)*10
2: wRtn=wRtn+(str[5 - len + i]-48)*100
3: wRtn=wRtn+(str[5 - len + i]-48)*1_000
4: wRtn=wRtn+(str[5 - len + i]-48)*10_000
end case
end loop
end function
Thank you.
Kind regards,
Filipe Santos