Convert Ascii Digits String to word var help

18 views
Skip to first unread message

flyway38

unread,
Jan 21, 2023, 1:57:45 PM1/21/23
to jallib
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

vsurducan

unread,
Jan 22, 2023, 12:36:08 AM1/22/23
to jal...@googlegroups.com
Hi Fellipe, 
If the ascii string content is numbers 0 to 9, then perhaps you can use function string_to_ dword from strings.jal library. 

However I'm not sure is better than yours. It depends of what you try to hit: best structured source code, fastest hex code or lowest hw stack.
best wishes,

--
You received this message because you are subscribed to the Google Groups "jallib" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallib+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jallib/f3e04583-4ea7-4302-9273-220361de6874n%40googlegroups.com.

flyway38

unread,
Jan 22, 2023, 1:46:48 PM1/22/23
to jallib
Hello Vasile,

Thank you for your input.
Will check that LIB.

Cheers,
FS
Reply all
Reply to author
Forward
0 new messages