Difference between revisions of "SHIP:Sail:bytesToString"

From Serious Documentation
Jump to: navigation, search
Line 23: Line 23:
 
|number||{{DataType|Float}}<br/>{{DataType|Integer}}<br/>{{DataType|Short}}<br/>{{DataType|Byte}}||Number to convert.<br/>The size of the source type drives the maximum number of bytes to convert; constants are assumed to have the least number of representation bytes.
 
|number||{{DataType|Float}}<br/>{{DataType|Integer}}<br/>{{DataType|Short}}<br/>{{DataType|Byte}}||Number to convert.<br/>The size of the source type drives the maximum number of bytes to convert; constants are assumed to have the least number of representation bytes.
 
|-
 
|-
|byteOrder||{{DataType|ByteOrder}}||Byte order to analyze the source number with (optional)
+
|byteOrder||{{DataType|ByteOrder}}||Byte order to analyze the source number with; default is {{Reserved|BYTEORDER.BIG_ENDIAN}} (optional)
 
|-
 
|-
 
|replacement||{{DataType|Codepoint}}||If a non-printable character (<code>0x01-0x1F</code>) is encountered, this UTF8 {{DataType|Codepoint}} is inserted into the string instead. <code>0x00</code> is permitted here and terminates the string upon encountering a non-printable byte. Any Unicode {{DataType|Codepoint}} is permitted.
 
|replacement||{{DataType|Codepoint}}||If a non-printable character (<code>0x01-0x1F</code>) is encountered, this UTF8 {{DataType|Codepoint}} is inserted into the string instead. <code>0x00</code> is permitted here and terminates the string upon encountering a non-printable byte. Any Unicode {{DataType|Codepoint}} is permitted.

Revision as of 10:16, 5 June 2015

Function Returns Introduced Description
bytesToString String v5.0.207 Builds a string from the ASCII bytes of an Integer, Short, or Byte until a 0x00 byte is encountered or the number of bytes of the size of the object are consumed. Optionally, non-printable characters can be replaced as-encountered. All byte orderings are supported.

See Also:

Prototypes

String bytesToString(Float/Integer/Short/Byte number);

String bytesToString(Float/{DataType|Integer}}/Short/Byte number, ByteOrder byteOrder);

String bytesToString(Float/{DataType|Integer}}/Short/Byte number, ByteOrder byteOrder, Codepoint replacement );

Parameters/Return Value

Parameter Data Type Description
number Float
Integer
Short
Byte
Number to convert.
The size of the source type drives the maximum number of bytes to convert; constants are assumed to have the least number of representation bytes.
byteOrder ByteOrder Byte order to analyze the source number with; default is BYTEORDER.BIG_ENDIAN (optional)
replacement Codepoint If a non-printable character (0x01-0x1F) is encountered, this UTF8 Codepoint is inserted into the string instead. 0x00 is permitted here and terminates the string upon encountering a non-printable byte. Any Unicode Codepoint is permitted.
Return String Character string

Detailed Description

The bytesToString() function returns the character string representing the 8, 16, or 32-bit ASCII byte sequence held in the number specified.

For example, the 32-bit integer 0x53484950 is a big endian order representation of "SHIP".

The most common use of this function is when receiving Modbus or other communications variables from a remote system that are encoding strings or character sequences. For example, the remote end may pass Short variables with 2-letter codes describing the state of a machine; say 0x444F or "DO" for Door Open. You may want to display these codes in your GUI, and so converting them to a String is desirable. One option in this example is

String = codepointToString((x>>8)&0x0FF) + codepointToString(x&0x0FF);

But this technique gets cumbersome with Integer variables. Further, what if the encoding in the number has non-printable characters you wish to replace, and/or you may encounter a string-ending 0x00 byte in the sequence? The method above does not handle these cases.

The bytesToString() function makes parsing these objects much simpler.

Size of Source Object

The size of the source number is analyzed and used as the byte count to analyze. Integer properties are 4 bytes, Short properties are 2 bytes, and Byte properties are 2 bytes. Floats are always trunc'd and passed as full 32-bit sized Integers. Constants are assumed to be the smallest representation size (1,2, or 4 bytes), so a constant of 0x80 is assumed to be 1 byte, a constant of 0x0123 is 2 bytes, and so forth. A constant written 0x00000023 is still assumed to be 1 byte, as is a constant 0xFFFFFFFF since math in SHIP is signed and -1 can be written as simply 0xFF.

Byte Ordering

Often the bytes in the number object are not in the same order. Often communications Integer values come in big Endian, but other times you may have all sorts of needs for the 4 different byte orderings of a 32-bit value (or 2 for a 16-bit value).

The optional parameter allows you to manipulate the byte ordering that the object is processed in. The ByteOrder value is similar to that supported in linkset nodes to convert incoming data, and a typical value might be BYTEORDER.SWAP8 to swap pairs of 8-bit values in a 16- or 32-bit number.

Examples

Example Result Notes
bytesToString(0x53); "S"
codepointToString(0x53484950); "SHIP"
codepointToString(0x53484900); "SHI"
codepointToString(0x53480050); "SH" Early terminated when 0x00 encountered in 3rd byte.
codepointToString(0x53484950,BYTEORDER.LITTLE_ENDIAN); "PIHS" All 4 bytes endian flipped.
codepointToString(0x53484950,BYTEORDER.BIG_ENDIAN,0x3F); "SHIP" All bytes are printable, so the replacement character 3F ('?') is unused.
codepointToString(0x53480350,BYTEORDER.BIG_ENDIAN,0x3F); "SH?P" The third byte, 0x03 is unprintable, so it is replaced with the replacement character 3F ('?').
codepointToString(0x53480350,BYTEORDER.BIG_ENDIAN,0x3F); "SH╳P" The third byte, 0x03 is unprintable, so it is replaced with the extended UTF8 replacement character 0x2573, the diagonal cross ('╳'). Note for this to be visible in your GUI this character must be present in your font.

References