|
|||||||||
|
|||||||||
Related Syntax | |||||||||
Data type conversion |
You can convert data from one type to another in OmniMark in a number of ways.
You can convert a string to an integer value simply by using the string value where an integer is expected. OmniMark coerces the string value to an integer automatically. The string used must contain only decimal digits.
; stream-to-integer.xom process local stream foo initial {"6"} local integer bar initial {7} set bar to bar + foo output "Bar = " || "d" % bar ; Output: Bar = 13
You can convert a string representation of a BCD number to a BCD by preceding the string value with the keyword bcd
:
; stream-to-bcd.xom include "ombcd.xin" process local stream foo initial {"12.75"} local bcd bar initial {"0.25"} set bar to bar + bcd foo output "Bar = " || "d" % bar ; Output: Bar = 13
You can convert a string representation of a floating point number to a float OMX type using float
.
; stream-to-float.xom include "omfloat.xin" process local stream foo initial {"6565.32e+4"} local float bar set bar to float foo output "Bar = " || "d" % bar ; Output: "Bar = 65653200"
You can convert an integer to a string expression using the "d" format item.
; integer-to-string.xom process local integer foo initial {6} output "Foo formatted as a string: " || "d" % foo ; Output: "Foo formatted as a string: 6"
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 "Integer formatted as string with hex, uppercase, 2 digits, padded: " || "16ru2fzd" % foo ; Output: "Integer formatted as string with hex, uppercase, 2 digits, padded: FD"
You can convert a BCD value to a string using the BCD template formatting language.
For instance, the following code outputs "$5,729.95":
include "ombcd.xin" process local bcd total set total to bcd 5729.95 output "<$,NNZ.ZZ>" % total
You can convert a floating point value to a string using float
:
include "omfloat.xin" process local stream foo initial {"6565.32e+4"} local float bar set bar to float foo output "d" % bar ; Output: 65653200.000000000000000
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 || "." ; Output: "The character corresponding to ASCII code 71 is G."
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"
You can convert an integer to a BCD by simply putting "bcd" before the integer name, as shown in this program:
; integer-to-bcd.xom include "ombcd.xin" process local integer dollars initial {2} local bcd total set total to bcd dollars + bcd 729.95 output "<$NNZ.ZZ>" % total ; Output: "$731.95"
You can convert an integer to a floating point data type by simply putting "float" before the integer name, as shown in this program:
; integer-to-float.xom include "omfloat.xin" process local integer dollars initial {2} local float total set total to float dollars + float 729.95 output "$" || "d" % total ; Output: "$731.95"
For information on how to represent hex numbers, octal numbers and others in which you convert a string to a number, see base.
Related Syntax base binary |
---- |