|
|||||
|
|||||
Related Topics | |||||
Data type conversion |
You can convert data from one type to another in OmniMark in a number of ways.
%
)
binary
operator
base
operator
The following examples show how these facilities can be applied to a number of different data conversion problems.
You can convert a string to an integer value simply by using the string value where an integer is expected. OmniMark calls its built-in string to integer conversion function to convert the string value to an integer automatically. The string used must contain only decimal digits.
process local stream foo initial {"6"} local integer bar initial {7} set bar to bar + foo output "Bar = " || "d" % bar
OmniMark's floating point and BCD libraries provide conversion functions for converting from strings to float and BCD numbers respectively, so strings are converted to floats or BCDs in the same way:
import "ombcd.xmd" unprefixed process local stream foo initial {"12.75"} local bcd bar initial {"0.25"} set bar to bar + foo output "bar = " || "d" % bar
You can convert an integer to a string expression using the "d" format item, as illustrated in the examples above.
The "d" format item has many format modifiers that allow you to specify how the number is displayed. For instance, to display a number as two hexadecimal digits, you would use the sequence "16ru2fzd". This sequence means:
Thus the following code will print "FD":
process local integer foo initial {253} output "16ru2fzd" % foo
You can convert a BCD value to a string using the BCD template formatting language.
For instance, the following code outputs "$5,729.95":
import "ombcd.xmd" unprefixed process local bcd total set total to 5729.95 output "<$,NNZ.ZZ>" % total
To get the ASCII code (or EBCDIC code on machines that use it) for an individual character, you can use the binary
operator:
process local stream foo initial {"G"} output "The ASCII code for " || foo || " is " || "d" % binary foo || "."
To output the character that corresponds to an ASCII code, use the "b" format item:
process local integer foo initial {71} output "The character corresponding to ASCII code " || "d" % foo || " is " || "b" % foo || "."
You can convert non-base 10 numbers, represented as strings, into integers using the base
operator. For instance, this program converts the string representation of a hexadecimal value to an integer:
process local stream hex-value initial {"7F"} local integer foo set foo to hex-value base 16 output "d" % foo
In some cases, OmniMark cannot tell which data format is intended when you provide a value of a different type. In this case, OmniMark cannot call the appropriate conversion function and you must specify which type you intended using a cast. A common example of this can occur when using overloaded operators. For example, the BCD and float libraries both provide overloaded versions of the +
operator to work with BCD and Float values respectively, and also to work with combinations of BCD or Float values with OmniMark's built-in types.
In the following example, an integer variable is a added to a string value that expresses a decimal fraction. The result is then assigned to a BCD variable. Because overloaded functions are selected based on the types of their arguments, and not on the type of their return values, OmniMark sees this as the addition of an integer with a string. It then throws an exception complaining that the string "729.95" is not a valid integer.
import "ombcd.xmd" unprefixed process local integer apples initial {2} local stream oranges initial {"729.95"} local bcd total set total to apples + oranges output "<$NNZ.ZZ>" % total
To force OmniMark to select the BCD version of the +
operator we must force at least one of the terms to be evaluated as a bcd value by using a cast:
import "ombcd.xmd" unprefixed process local integer apples initial {2} local stream oranges initial {"729.95"} local bcd total set total to apples + bcd oranges output "<$NNZ.ZZ>" % total
If you create your own data types using records, you may want to write conversion functions to convert between those types and other types. In particular it is often useful to convert between user defined types and strings. See conversion functions.
Here is a simple hex dump program that uses some of these conversion methods to print out side-by-side ASCII and hex representations of a file. In the ASCII representation, unprintable characters are represented by periods:
declare #main-input has binary-mode macro format-2digit-hex is "16ru2fzd" macro-end process submit #main-input find any{1 to 16} => chars local integer i initial {0} repeat scan chars match [" " to "~"]+ => visible output visible match any output "." again output " " ||* (16 - length of chars) repeat scan chars match any => char output " " || format-2digit-hex % binary char increment i output " -" when i=8 again output "%n"
Related Topics
|
Copyright © Stilo International plc, 1988-2010.