7.4 String and number conversion

Hollywood supports automatic string to number and number to string conversion. That means that if a function expects a string in an argument and you pass a number then Hollywood will automatically convert this number into a string and pass it to the function as a string.

For example: StrLen() returns the length of the specified string. Now if we call

a = StrLen(256)

Hollywood will automatically convert the number 256 to the string "256" and therefore the variable a receives the value of 3 because the string "256" consists of three characters.

This works the same way vice versa. If you pass a string to a function that expects a number then Hollywood will try to convert this string to a number. The difference to the number to string conversion is now that the string to number conversion might fail. For example: Hollywood cannot convert a string like "Hello" to a number. The string must contain decimal or hexadecimal digits only. Mixed alphabetical and number strings cannot be converted either, even if the digits come before the characters. Hexadecimal numbers must be prefixed with a dollar sign ($) or 0x. An example:

LoadBrush("1", "Brush.iff")

LoadBrush() expects a number as the identifier. Thus, the string "1" will be automatically converted to a number by Hollywood. This in contrast will not work:

LoadBrush("Test", "Brush.iff")
LoadBrush("1Test", "Brush.iff")

The strings "Test" or "1Test" cannot be converted to a number.

You can also use all of the operators with numbers and strings except the relational operators. They can only compare two values of the same data type. For example, the following code works fine:

a = "5" * 10 + 100 / "10" + ("100" - 60)  ; a is 100

But this code will give you an error because you use relational operators with values of different types:

If "10" < 20       ---> Error!

If you want to do something like this, you have to use Val() or StrStr() to convert the number manually to string or the string to number. E.g.

If Val("10") < 20  ---> Works!

Show TOC