Data type conversion

You can convert data from one type to another in OmniMark in a number of ways:

  • by taking advantage of OmniMark's built in conversion functions,
  • by casting a shelf item of one type to another type,
  • by writing your own conversion functions,
  • by writing conventional functions to convert data,
  • using the format operator (%),
  • using the binary operator, and
  • using the base operator.

The following examples show how these facilities can be applied to a number of different data conversion problems.

String-to-number conversion

You can convert a string to an integer value simply by using the string value where an integer is expected. OmniMark invokes a built-in conversion function to convert the string value to an integer automatically. The string used must contain only decimal digits.

  process
     local string  s initial { "6" }
     local integer i initial { 7 }
  
     set i to i + s
     output "i = " || "d" % i
          

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 string s initial { "12.75" }
     local bcd x    initial { "0.25" }
  
     set x to x +  s
     output "x = " || "d" % x
          

Number-to-string conversion

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:

  • 16r - display using radix (or base) 16—hexadecimal,
  • u - display using uppercase letters for digits over 9,
  • 2f - display with a width of 2 digits,
  • z - pad the display with leading zeros, and
  • d - the d format item.

Thus the following code will print FD:

  process
     local integer i initial { 253 }
  
     output "16ru2fzd" % i
          

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 initial { 5729.95 }
  
     output "<$,NNZ.ZZ>" % total
          

Converting to and from ASCII values

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 string s initial {"G"}
  
     output "The ASCII code for " || s || " is "
         || "d" % binary s || "."
          

To output the character that corresponds to an ASCII code, use the b format item:

  process
     local integer i initial { 71 }
  
     output "The character corresponding to ASCII code " || "d" % i || " is "
         || "b" % i || "."
          

Dealing with non-base 10 numbers

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 string  s initial { "7F" }
     local integer i
  
     set i to s base 16
     output "d" % i
          

Casting

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 value is a added to a string value that expresses a decimal fraction. The result is then assigned to a BCD shelf item. 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 i initial { 2 }
     local string  s initial { "729.95" }
     local bcd     x
  
     set x to i + s
     output "<$NNZ.ZZ>" % x
          

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 i initial { 2 }
     local string  s initial { "729.95" }
     local bcd     x
  
     set x to i + bcd s
     output "<$NNZ.ZZ>" % x
          

Conversion functions

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.

A simple hexadecimal dump program

Here is a simple hexadecimal dump program that uses some of these conversion methods to print out side-by-side ASCII and hexadecimal representations of a file. In the ASCII representation, unprintable characters are represented by periods:

  declare #main-input has binary-mode
  
  process
      submit #main-input
  
  
  find any{1 to 16} => chars
     local integer i
  
     repeat scan chars
     match [" " to "~"]+ => visible
        output visible
  
     match any
        output "."
     again
  
     output " " ||* 16 - length of chars
  
     repeat scan chars
     match any => char
        output " " || "16ru2fzd" % binary char
        increment i
        output " -" 
           when i = 8
     again
     output "%n"
          

Prerequisite Concepts