Number Parsing in Modelica
Our example demonstrates how to parse numbers from strings in Modelica. This is a common task in many programs.
In Modelica, we use the Modelica.Utilities.Strings
library for parsing strings into numbers. Here’s a breakdown of the operations:
- We use
Strings.scanReal()
to parse floating-point numbers. Strings.scanInteger()
is used to parse integers. By default, it assumes base 10.- For hexadecimal numbers, we can specify the base (16) as the second argument to
Strings.scanInteger()
. - Modelica doesn’t have a separate unsigned integer type, so we use
scanInteger()
for both signed and unsigned integers. - There’s no direct equivalent to
Atoi()
, butscanInteger()
serves the same purpose for basic base-10 integer parsing. - Error handling in Modelica is typically done using the
when
clause. If an invalid string is provided, it will trigger an event and print an error message.
Note that Modelica is primarily used for modeling and simulation, so its string parsing capabilities might be more limited compared to general-purpose programming languages. The exact behavior and available functions may vary depending on the Modelica implementation you’re using.
To run this model, you would typically use a Modelica simulation environment. The output would depend on how the simulation is set up, but it should print the parsed numbers in order.
Next, we’ll look at another common parsing task: URLs.