Converting a String to an Integer and Back Again – Sikuli
Recently, while experimenting with Sikuli, and asking questions in the forums, I ran across a helpful tidbit: How to convert a string to an integer and back again. The process is quite simple. If you are, say, keeping track of how many laps a hypothetical hippo has run around a hypothetical car in a hypothetical computer program, you can use a variable to record an integer in Sikuli. At the beginning of the script, you would define the integer, and, depending on what you’re tracking, you would define the base value. If we’re tracking Mr. Hippo, we would start at zero, as he would begin running at zero laps, he wouldn’t have already completed one lap when he starts running around the car. If that’s case, we would define our base value as zero.
Notice, I didn’t enclose the ’0′ with quotation marks, because we’re recording an integer, not a string. Examples:
Integer: “123″
String: “The quick brown hippo jumped over the lazy car.”
Sikuli can determine if we are asking for an integer or string simply from the quotation marks. If they don’t exist around a base-10 numerical value, it defines it as an integer. If they do exist around a base-10 numerical value, or, for that matter, any letters or numbers in the define statement, Sikuli defines that as a string. In our sample Sikuli project, we will start with defining an integer as ’0′, and create a prompt, asking how many laps Mr. Hippo has completed, which will be written to the defined integer, and then printed in a popup. Keep in mind that a popup can only display a string, so the number of laps will need to be converted back to a string.
while loop == 1:
mr_hippo_laps = input("How many laps has Mr. Hippo completed?")
try:
mr_hippo_laps = (int(mr_hippo_laps))
except:
popup("The entered string cannot be converted to a valid base 10 integer. The script will now exit")
exit()
popup("Please click the 'OK' button to see how many laps Mr.Hippo has completed.")
popup(str(mr_hippo_laps))
while loop == 0:
exit()
We’re done! We now have a simple Mr. Hippo laps tracker in Sikuli.
Thanks your for help. Saying and doing are two things
Exactly what I was looking for! This isn’t documented anywhere on the Sikuli site, but a quick Google search brought me here. Thanks a bunch!