Conversion to string or number

Verified
Added by iNTERFACEWARE

How to convert numbers and node trees to a to string representation, and how to convert a numeric strings to numbers

Source Code
   -- convert a number to a string
   local number = 1234
   local string = tostring(number)
   trace(number, string)
   
   -- convert a numeric string to a number
   local string = '4321'
   local number = tonumber(string)
   trace(string, number)
   
   -- convert an HL7 node tree to a string (very common usage)
   -- works with all node trees types (HL7, XML etc)
   
   -- need a parsed HL7 message to use
   local Msg = hl7.parse{vmd = 'example/demo.vmd', data = Data} --    
   -- using tostring()
   local hl7String = tostring(Msg)
   -- the :S() function is a builtin shorthand to tostring()
   local hl7String = Msg:S()
   trace(Msg, hl7String)
Description
How to convert numbers and node trees to a to string representation, and how to convert a numeric strings to numbers
Usage Details

To convert numbers or node trees (HL7, XML etc) to strings use tostring() or the builtin :S()  function (a shorthand for tostring). To convert from numeric strings to numbers use tonumber().

How to use the snippet:

  • Paste the desired conversion into your script

Note: If you want to convert a leaf node in a node tree you will probably want to use :nodeValue() instead of tostring() or :S().