变量类型转换
15.4.1 Variable type conversion
15.4.1.1 ByteToStr
Explanation |
It is used to convert byte-type data to string-type data in a specified format. |
Definition |
Return value, data type: string, the converted string-type data. ByteToStr (BitData [\Hex] | [\Okt] | [\Bin] | [\Char]); BitData, data type: byte, the byte-type data to be converted; convert by decimal by default. \Hex, identifier, converted in hexadecimal. \Okt, identifier, converted in octonary. \Bin, identifier, converted in binary. \Char, identifier, converted according to Ascii character format. |
Example |
Example 1 VAR byte data1 = 122 VAR string str1 str1 = ByteToStr(data1); //”122” str1 = ByteToStr(data1 +Hex); //”7A” str1 = ByteToStr(data1 +Okt); //”172” str1 = ByteToStr(data1 +Bin); //”01111010” str1 = ByteToStr(data1 +Char); //”z” Define byte-type variable data1 and assign it with 122, convert data1 to string-type data: 122 by decimal; 7A by hexadecimal;172 by octal; 01111010 by binary; and z by character. |
15.4.1.2 DecToHex
Explanation |
It is used to convert a decimal number to a hexadecimal number. |
Definition |
Return value, data type: string, the hexadecimal data obtained from the conversion, represented by 0-9, a-f, A-F. DecToHex(str); str, data type: string, the decimal data to be converted, represented by 0-9. |
Attention |
Data range from 0 to 2147483647 or 0 to 7ffffffff. |
15.4.1.3 DoubleToByte
Explanation |
It is used to convert a double-type variable or a double array to a byte array. |
Definition |
Return value, data type: byte array, the byte array obtained from the conversion, each double data is converted to 8 byte-type data. DoubleToByte(dou1); dou1, data type: double, the double-type variable to be converted. |
15.4.1.4 DoubleToStr
Explanation |
It is used to convert a double-type variable to a string. |
Definition |
DoubleToStr(Val, Dec); Val1, data type: double, the double-type variable to be converted. Dec, data type: string, the number of decimal places to be kept. |
Attention |
The maximum number of decimal places is 15 digits. |
15.4.1.5 HexToDec
Explanation |
It is used to convert a hexadecimal number to a decimal number. |
Definition |
Return value, decimal Integer data obtained from the conversion, represented by 0-9. HexToDec(str); str, data type: string, the hexadecimal data to be converted, represented by 0-9, a-f, A-F. |
Attention |
Data range from 0 to 2147483647 or 0 to 7ffffffff. |
15.4.1.6 IntToByte
Explanation |
It is used to convert an int-type variable or an int array to a byte array. |
Definition |
Return value, the byte array obtained from the conversion, each int data converted to four byte data. Data type: byte array. IntToByte(int1); int1, data type: int or int array, the int-type variable or int array to be converted. |
Attention |
Data range from -2147483647 to 2147483647. |
15.4.1.7 IntToStr
Explanation |
It is used to convert integer to string. |
Definition |
Return value, the string obtained from the conversion. IntToStr(int1); int1, data type: int, the integer to be converted. |
Attention |
Data range from -2147483647 to 2147483647. |
15.4.1.8 EulerToQuaternion
Explanation |
It is used to convert Euler angle to quaternion. |
Definition |
Return value, the conversion result, 0 means successful, others mean abnormal. EulerToQuaternion (type, A, B, C, q1, q2, q3, q4); Type, Euler angle order type, including EULER_XYZ and EULER_ZYX. A, B, C, the Euler angle to be converted. Data type: double q1~q4, the quaternion obtained from the conversion. Data type: double |
15.4.1.9 QuaternionToEuler
Explanation |
It is used to convert a quaternion to an Euler angle. |
Definition |
Return value, the conversion result, 0 means successful, others mean abnormal. QuaternionToEuler (type, q1, q2, q3, q4, A, B, C); Type, Euler angle order type, including EULER_XYZ and EULER_ZYX. q1~q4, the quaternion to be converted. Data type: double A, B, C, the Euler angle to be converted. Data type: double |
15.4.1.10 DataToBytes
Explanation |
Convert a variable of a specified data type into a byte array (or a single byte variable) and store it in the target byte variable or array. |
Definition |
DataToBytes(byte_var, index, data, type); Convert the variable data to the type specified by type and store its byte representation in byte_var (starting from index). byte_var, store the converted byte data. Data type: byte variable or byte array. If it is a single byte variable, the index must be 1, and only one-dimensional arrays are supported. index, initial index (index>0). Data type: int. data, data to be converted. Data type: double/int/string/bool. Array type is not supported. type, conversion type. Data type: string. Optional values (case-insensitive): "Double", "Float", "Str", "Int16", "Int32", "Int64", "Bool8", "Bool16" |
Example |
GLOBAL PROC main() //Example 1: Store the conversion result in a byte variable byte byte++_++var; bool b1 = true; DataToBytes(byte++_++var, 1, b1, "bool8"); //Convert b1 to a byte as Bool8 type and store it in byte++_++var //Example 2: Store the conversion result in a byte array byte byte++_++array; double d1 = 10.234; DataToBytes(byte++_++array, 1, d1, "double"); //Convert d1 as Double type and store it starting from index 1 string s1 = "hello"; DataToBytes(byte++_++array, 9, s1, "Str"); //Convert s1 as a string and store it starting from index 9 ENDPROC |
Attention |
Explanation of the number of bytes for each conversion type (64-bit machine): double, 8 bytes; float, 4 bytes; int16, 2 bytes; int32, 4 bytes; int64, 8 bytes; bool8, 1 byte; bool16, 2 bytes. Byte arrangement order follows little-endian mode (lower-order bytes are stored at lower array indices). The conversion type strings are case-insensitive; for example, "double", "DOUBLE", and "Double" are all valid inputs. If the variable 'data' is incompatible with the type specified by 'type' and cannot be implicitly converted, a runtime error will be reported. If the converted byte data cannot be stored in 'byte_var' (such as due to out-of-range index or insufficient target storage space), a runtime error will occur. This command is triggered during lookahead and skipped during step back. |
15.4.1.11 BytesToData
Explanation |
Convert a byte variable or byte array to a variable of a specified data type. |
Definition |
Return value, the conversion result, 0 means successful, others mean abnormal. QuaternionToEuler (type, q1, q2, q3, q4, A, B, C); Type, Euler angle order type, including EULER_XYZ and EULER_ZYX. q1~q4, the quaternion to be converted. Data type: double A, B, C, the Euler angle to be converted. Data type: double BytesToData(byte_var, index, data, type); Starting from the specified index of byte_var, extract the corresponding number of bytes based on the data type specified by type, convert them to that type, and store the result in data. byte_var, data to be converted. Data type: byte variable or byte array. If it is a single byte variable, the index must be 1, and only one-dimensional arrays are supported. index, initial index (index>0). Data type: int. data, convert result variables. Data type: double/int/string/bool. Array type is not supported. type, conversion type. Data type: string. Optional values (case-insensitive): "Double", "Float", "Str", "Int16", "Int32", "Int64", "Bool8", "Bool16" |
Example |
GLOBAL PROC main() //Example 1: Convert byte variable to bool variable byte byte++_++var = 0; bool b1 = true; BytesToData(byte++_++var, 1, b1, "Bool8"); //Convert byte++_++var as Bool8 type and store the result in b1 print(b1); // FALSE //Example 2: Extract different types of data from a byte array byte byte++_++array; double d1; BytesToData(byte++_++array, 1, d1, "Double"); //Read 8 bytes starting from index 1, convert to double type and store in d1 string s1; BytesToData(byte++_++array, 9, s1, "Str:5"); //Read 5 bytes starting from index 9, convert to a string and store in s1 ENDPROC This command is usually used in conjunction with DataToBytes. For example: Use the DataToBytes command to convert data into a byte array, and then restore it to a variable using the BytesToData command. GLOBAL PROC main() byte byte++_++array; double d1 = 10.234; DataToBytes(byte++_++array, 1, d1, "double"); string s1 = "hello"; DataToBytes(byte++_++array, 9, s1, "STR"); int i1 = 10; DataToBytes(byte++_++array, 14, i1, "int32"); bool b1 = true; DataToBytes(byte++_++array, 18, b1, "bool8"); double d1++_++res; BytesToData(byte++_++array, 1, d1++_++res, "double"); print(d1++_++res); //Output: 10.234 string s1++_++res; BytesToData(byte++_++array, 9, s1++_++res, "STR:5"); print(s1++_++res); //Output: hello int i1++_++res; BytesToData(byte++_++array, 14, i1++_++res, "int32"); print(i1++_++res); //Output: 10 bool b1++_++res; BytesToData(byte++_++array, 18, b1++_++res, "bool8"); print(b1++_++res); //Output: TRUE ENDPROC |
Attention |
When converting to a string type, due to the inability to automatically infer the length of the string, it is necessary to explicitly specify the number of bytes in the 'type' parameter using the 'Str: length' format. For example, 'Str: 5' means reading 5 bytes and converting them into a string. If a sufficient number of bytes cannot be read from 'byte_var' to complete the specified type conversion, an error will be triggered. If the variable 'data' is incompatible with the type specified by 'type' and cannot be implicitly converted, a runtime error will be reported. This command is triggered during lookahead and skipped during step back. Before performing type conversion, please confirm whether the value range of the data to be converted falls within the representation range of the target type. The conversion between floating-point types (such as Double to Float) may cause slight errors due to precision differences. Special attention shall be paid to the risk of overflow when the range of conversions between integers narrows. |
15.4.1.12 ClearRawBytes
Explanation |
Clear all contents of the byte array to 0 |
Definition |
ClearRawBytes(bytes); bytes, byte variables/arrays. |
Example |
byte arr[10]; ClearRawBytes(arr); //Clear the arr array and set all to 0. |
15.4.1.13 CopyBytes
Explanation |
Used for copying byte arrays |
Definition |
CopyBytes(dst_byte, src_byte, cpy_size, ); Similar to the memcpy command, copy the src_byte array to the dst_byte array. The number of copies and array offset values can be specified through cpy_size, dst_bias, and src_bias dst_byte, target byte array. src_byte, source byte array. cpy_size, number of copies Data type: int. dst_bias, write from the index dst_bias+1 of the target bytes array. Data type: int. Optional parameter, default to 0. src_bias, copy from the index dst_bias+1 of the source bytes array. Data type: int. Optional parameter, default to 0. |
Example |
Example 1 byte arr1 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; byte arr2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; CopyBytes(arr1, arr2, 10); //Complete copy print(arr1); //Output: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} print(arr2); //Output: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} Example 2 byte arr1 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; byte arr2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //Starting from the third element of arr2, copy 5 elements, and then start writing from the second element of arr1 CopyBytes(arr1, arr2, 5, 1, 2); print(arr1); // {0, 3, 4, 5, 6, 7, 0, 0, 0, 0 } print(arr2); // {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } |