Efficiently Convert SINT Hex Array to Decimal as String in RSLogix 5000

Question:

Hello there! I need some guidance on how to convert an array of SINT[5] values into a Decimal format. The combined value of the array consists of 40 bits, which is too large for a DINT to accommodate. My objective is to obtain the Decimal value as a STRING within RSLogix 5000 v24. For instance, let's take an example with a Hex Array where SINT[0]=01, SINT[1]=2A, SINT[2]=64, SINT[3]=E0, and SINT[4]=CE. In Excel, you can use the function HEX2DEC(012A64E0CE) to calculate the value, which results in 5006221518. This process helps in efficiently converting hexadecimal values into their Decimal equivalents.

Top Replies

Employ bit shifting to transfer 4 pairs into a DINT for rapid transformation. Next, combine a real number with CPT to incorporate the leading pair multiplied by 16 to the power of 8 and 9.

It appears that the PLC may not support the necessary number of mantissa bits for REALs, as 40 bits are needed but only 24 are available in a REAL data type. Are LREALs supported by the target PLC? Additionally, clarification is needed on whether the 40-bit value is treated as a signed or unsigned integer. Furthermore, the byte order of the target PLC must be determined. The choice of programming language is also important to consider. While working with LREALs (and possibly UDINTs), having answers to these questions is essential for successful implementation. It should be noted that the data type in question is a SINT array, not a HEX array; HEX arrays do not exist.

In a recent discussion, drbitboy and @robertmeemeant discussed the limitations of using REAL data types in programmable logic controllers (PLCs). It appears that PLCs may not have access to the LREAL data type, which is necessary for handling 40-bit values. Additionally, they raised questions about the byte order, programming language compatibility, and the need to use alternative data types like UDINTs for such calculations. It was clarified that there is no specific HEX array, but rather a SINT array representing hexadecimal values. To address the challenge of handling 40-bit values, a strategy was proposed involving splitting the calculation into manageable parts. By breaking down the process and manipulating DINTs, the goal is to accurately convert a 40-bit HEX value into a 10-digit decimal number. The proposed method involves transferring bits from the SINT array to the DINT, analyzing specific bits to extract different portions of the decimal value, and combining the results to form the final 10-digit number. While the exact instructions for this logic are still being developed, the plan includes utilizing mathematical operations to extract and concatenate the necessary digits. In conclusion, the team is optimistic about the feasibility of this approach and believes that with careful planning and execution, the conversion process can be successfully achieved.

To improve the efficiency of your code, consider treating your 5-byte value as a 64-bit one. Search for an algorithm that performs 64-bit division within 32-bit registers and integrate it into an AOI design. These algorithms are widely available, as even Knuth dedicated a section to them due to their prevalence. By repeatedly dividing by 10 and referencing an ASCII table for the results, you can simplify your task. To optimize your AOI for handling 64-bit values divided by an 8-bit divisor, focus on the most common algorithm which involves a 64-bit dividend and a 32-bit divisor.

A five-byte integer consists of forty bits, allowing for unsigned values ranging from 0 to just over one trillion (up to 13 decimal digits). To achieve this, three AOIs can be utilized: 1) An adder AOI that takes two single-decimal-digit SINT inputs, processes them with a decimal carry in and out, and outputs a single SINT value. 2) A 13-decimal-digit adder AOI that utilizes the single-digit adder and processes two 13-digit SINT array inputs to produce a 13-digit SINT array output. 3) A conversion AOI that transforms a 5-element SINT array binary value into a STRING with its decimal representation. This AOI internally operates using two 13-element SINT arrays: Result (initialized to all 0s) and Increment (initialized with a value of 1 in the least-significant digit and 0s elsewhere). For each of the forty input bits, Increment is added to Result if the bit value is zero. Regardless of the bit value, Increment is added to itself. Subsequently, 48 is added to each element of Result, these values are placed into the .DATA elements of the output STRING, and the .LEN attribute of the output STRING is set to 13. Removing any leading zeros is left for the user to handle. While not the most efficient solution, this approach is relatively straightforward, making it easy to code and debug.

Definitely, this can be tricky when dealing with large numbers! For RSLogix 5000, you could handle this by using the COP instruction to convert the SINT array to an LINT first, as LINT can accommodate 64 bits. After that, you would need a custom routine to convert that LINT value into a STRING. It's important to remember a direct conversion from hexadecimal to decimal (like HEX2DEC in Excel) isn't available in RSLogix enviroment so doing it step-wise becomes necessary. The hexadecimal values you've given as example will be treated bit by bit in this process to ensure accuracy in conversion. It might require some tinkering, but it should work smoothly!

Hey there! You're certainly on the right track but I understand that doing this in RSLogix 5000 is a bit different than in Excel. The first thing you might want to do is to use the COP instruction to copy your SINT array into a LINT variable. You need a LINT because as you noted, the combined SINT array is too large for a DINT. Once you have it in a LINT variable, I would use the STRING instruction to convert the LINT into a STRING. I hope that helps, let me know if you need more clarification on any of these steps.

I think you're on the right track! If you're skilled at using Structured Text (ST) inside of RSLogix, you could create a function that mimics your Excel example. Essentially, you would need to convert each SINT value into its hexadecimal equivalent, then concatenate those together into one large string. Once you have that, you could create a loop that moves through each digit of your string and for each character, multiplies the appropriate power of 16 (using the position of the digit) by the value of the digit (use a switch or nested-if statement to translate A=10, B=11, etc), then adds that product to a running total. This is a more manual way of converting hex to decimal, but I think it would produce the result you want. Practice with smaller hexadecimal numbers to make sure your logic is correct.

More Replies →

Streamline Your Asset Management
See How Oxmaint Works!!

✅   Work Order Management

✅   Asset Tracking

✅   Preventive Maintenance

✅   Inspection Report

We have received your information. We will share Schedule Demo details on your Mail Id.

To add a comment, please sign in or register if you haven't already..   

Frequently Asked Questions (FAQ)

FAQ: 1. How can I convert an array of SINT values to a Decimal format in RSLogix 5000?

Answer: To convert an array of SINT values to a Decimal format in RSLogix 5000, you can combine the individual SINT values and then convert the resulting Hex value to Decimal. This can be achieved by following a process similar to the HEX2DEC function in Excel.

FAQ: 2. Why is it necessary to convert a large combined value to a Decimal STRING in RSLogix 5000?

Answer: When the combined value of the SINT array exceeds the capacity of a DINT in RSLogix 5000, converting it to a Decimal STRING allows for efficient handling and manipulation of the large value within the program.

FAQ: 3. Can you provide an example of converting a Hex Array to a Decimal value in RSLogix 5000?

Answer: Suppose you have a Hex Array with values SINT[0]=01, SINT[1]=2A, SINT[2]=64, SINT[3]=E0, and SINT[4]=CE. By combining these values and converting the resulting Hex value to Decimal, you can obtain the equivalent Decimal value, such as 5006221518 using a similar process to the HEX2DEC function in Excel.

Ready to Simplify Maintenance?

Join hundreds of satisfied customers who have transformed their maintenance processes.
Sign up today and start optimizing your workflow.

Request Demo  â†’