Convert Hex to Binary

I've since thought about and improved it from an understandability view. But I'm not testing if a string of more than 8 hex digits is presented. Do you think I should keep the first 8, the last 8? I've written AOIs for my own programs but there I knew what I was giving it as arguments. Right now the thing will overflow. I'll think about it and maybe post some enhancement.
 
I know this is a very old thread. But started searching to see if i could find an AOI to convert from hex to decimal and found this thread.
I am integrating some PLC5 racks into RSLogix 5000 via RIO.
This is all somewhat temporary (for a year or so) as eventually I will be doing conversion racks and get rid of PLC5 cards all together. But in the mean time I have to use RIO. I already have a bunch of Remote racks via ControlNet and all the analog input values come back in decimal on those but on the RIO comes back in Hex. Some of these get messaged to other platforms and it all gets very confusing with some in hex and some in Decimal.

So finally to my question have you ever written an AOI or know were I could get one that will convert hex tag to a decimal tag? I have searched for a while and this thread is the closest thing I could find. Thanks!
 
I know this is a very old thread. But started searching to see if i could find an AOI to convert from hex to decimal and found this thread.
I am integrating some PLC5 racks into RSLogix 5000 via RIO.
This is all somewhat temporary (for a year or so) as eventually I will be doing conversion racks and get rid of PLC5 cards all together. But in the mean time I have to use RIO. I already have a bunch of Remote racks via ControlNet and all the analog input values come back in decimal on those but on the RIO comes back in Hex. Some of these get messaged to other platforms and it all gets very confusing with some in hex and some in Decimal.

So finally to my question have you ever written an AOI or know were I could get one that will convert hex tag to a decimal tag? I have searched for a while and this thread is the closest thing I could find. Thanks!

There are no such things as "hex tags" or "decimal tags", they are only display formats. Decimal and Hex data is stored in pure binary, and you can choose which format to display them in.

You may be confusing "Hex" data with "BCD" (Binary Coded Decimal) data, which certainly has a different storage structure. BCD data is predominantly used for thumbwheel switches and single digit displays stacked side-by-side, where each group of 4 bits represents a "digit" in the number.

eg.
decimal 35 in Binary is 0010_0011 (32 + 2 + 1)
decimal 35 in BCD is 0011_0101 ( "3", "5")

Logix5000 has the instructions TOD (To BCD), which you can use to convert Binary to BCD and FRD (From BCD) which converts BCD to Binary.
 
Thanks Daba.
Yeah I understand that the there is no such thing as a Hex tag or decimal its just a view thing. But with that being said when something is being messaged and it originates (or needs to be written to) in hex format it is different. Let me give a couple examples to better explain myself (I hope) and I have two different applications that have me looking for a solution.
Application one:
I have both analog input (1771-IFE) and analog output (1771-OFE) cards in a rack that I am using as remote I/O (RIO)
I have MSG instructions doing the BTW and BTR's for these to configure the cards and read and write values for the input and output cards. Word 5 and 6 for the BTW to one of the Input cards set the scale for this channel. The source for the MSG is a portion of a large array of INTs, not every word in the array is being the same way so its base view is decimal.
The minimum Scale is zero (decimal style: 0, Hex style: 0) that’s pretty simple being zero.
The maximum Scale is 4095 (decimal style: 16533, Hex style: 16#4095) that’s were it causes confusion, because the value I want to display is 4095. In this large program I have a mix now of ControlNet racks and RIO racks. I want all my data to look the same to help Maintenance and operations to be able to troubleshoot more efficiently.
Application two:
I have a couple RFID readers that I am installing to track metal pallets through flash cooler. The reader data coming back to the PLC is in a SINT array with each SINT being a two digit number hex (00-99) that I need to Concatenate together into one long number (dint or string I don’t really care which) to give me the pallet number (Each pallet has a unique 8 digit identifier). So I need to take the 16#99, 16#99 16#00 16#00 and make it read 00009999. How do I strip out 16# and the hex number only?
Does that make any sense? I've read it back and to me it makes sense but I wrote it so it should…..
 
Thanks Daba.
Yeah I understand that the there is no such thing as a Hex tag or decimal its just a view thing. But with that being said when something is being messaged and it originates (or needs to be written to) in hex format it is different. Let me give a couple examples to better explain myself (I hope) and I have two different applications that have me looking for a solution.
Application one:
I have both analog input (1771-IFE) and analog output (1771-OFE) cards in a rack that I am using as remote I/O (RIO)
I have MSG instructions doing the BTW and BTR's for these to configure the cards and read and write values for the input and output cards. Word 5 and 6 for the BTW to one of the Input cards set the scale for this channel. The source for the MSG is a portion of a large array of INTs, not every word in the array is being the same way so its base view is decimal.
The minimum Scale is zero (decimal style: 0, Hex style: 0) that’s pretty simple being zero.
The maximum Scale is 4095 (decimal style: 16533, Hex style: 16#4095) that’s were it causes confusion, because the value I want to display is 4095. In this large program I have a mix now of ControlNet racks and RIO racks. I want all my data to look the same to help Maintenance and operations to be able to troubleshoot more efficiently.
Application two:
I have a couple RFID readers that I am installing to track metal pallets through flash cooler. (1) The reader data coming back to the PLC is in a SINT array with each SINT being a two digit number hex (00-99) that I need to Concatenate together into one long number (dint or string I don’t really care which) to give me the pallet number (Each pallet has a unique 8 digit identifier). (2) So I need to take the 16#99, 16#99 16#00 16#00 and make it read 00009999. (2) How do I strip out 16# and the hex number only?
Does that make any sense? I've read it back and to me it makes sense but I wrote it so it should…..

Long post - bear with it....

Application 1.
I feel your pain... having worked on many "legacy" upgrades to ControlLogix where they retained the existing 1771 IO chassis, I came across the same issues. I resolved them all by creating UDTs for each BTW/BTR "flavour", naming the elements and component parts, and selecting the correct "View Style" for data that wasn't decimal. No need for the manual anymore, the UDT member descriptions can pass-thru onto your tags when viewing the logic, for your people to see numbers as they want to. You just need to be careful to "pad out" with dummy members where necessary to keep the UDT structure exactly matching the BTW/BTR INT array bits and bytes. I can't remember if I had to COP between UDT tags and plain vanilla INT arrays for the messages, or whether I just MSGd the UDT tag directly, I can't get at the code now....


Application 2.
Lets deal with a few points in your post, numbered as above....

(1)
00-99 doesn't look like a HEX number to me, it looks like the SINT is containing a 2-digit BCD code. You use the HEX View (16#) to display BCD numbers, because 0-9 is the same bit pattern in both numbering systems, confusing I know, but they didn't provide a "BCD" view, only hex (16#).

Even if they are HEX digits (which go from 0 to F, that's why there's 16 of them), the FRD in ControlLogix is flawed and incorrectly converts them to decimal, unlike the PLC5 and SLC which treats a BCD digit >9 as an overflow.

SLC :-

1001 FRD = 9
1010 FRD = 32767 and Overflow flag set
to
1111 FRD = 32767 and Overflow flag set

Logix5000 :-

1001 FRD = 9
1010 FRD = 10
to
1111 FRD = 15

This flaw might work to your advantage if the data is Hex, and not BCD.

(2)
You don't need to "strip" the 16#, it is put there by the programming software to tell you what number base you are viewing the number in. It is not contained in the data....

(3)
So the number you need to extract is stored as two digits in each of four SINTs, but it looks like they are in reverse order...

eg. for 00009999 decimal...
SINT[0] = 16#99
SINT[1] = 16#99
SINT[2] = 16#00
SINT[3] = 16#00

We can easily create a 32-bit BCD number from them using 8 BTD instructions, taking 4 bits at a time from the SINT array and putting them into 4-bit fields in a 32-bit DINT.

We won't need to be bothered if bit 31 gets turned on, since we will immediately convert the bit pattern to a decimal number with FRD.

Your BTD instructions will need to be coded to pick up the correct 4 bits from each SINT, since I can't tell (because you used example 00009999) if they are low-byte/high-byte or high-byte/low-byte orientated. You can easily see which by reading in a pallet number "12345678".

If it works out correct for your incoming data, the 8 BTD instructions can reduce to 4, moving 8 bits at a time.


Lastly, have you searched the knowledgebase for Add-On Instructions to do the conversion ? I'm sure you're not the first to need this.


 
Yeah, Right on Daba.
I was on the same track (slightly different but, end result the same) as you just posted.

I was actually getting on to say I think I got it figured out when I saw you posted. Thanks for that!

I did search knowledgebase for an AOI to do this but did not find anything.
Thanks for the Info I may try your way looks like it may be cleaner (less coding).
Thanks again,
Jim
 
Ok I didn't do anything about application one yet. Will look into doing UDT's (will have to make sure the Gods above approve of me changing our TAG creation SOP) They still like me to code like everything is a PLC5 (I hate it).

But here is what we are going to do about Application 2:
Structured txt routine (that I will end up putting in an existing AOI that pulls the data from the RFID reader, this will then output it to a format I want.)

if Ai <> AMaxi then
//Ai is the position or index of the array
//AMaxi is the max position of the array
For Ai:=0 to AMaxi-1 do
//Change Hex Array (AHex) to Decimal and put it in a Decimal Array (ADecimal)
if AHex[Ai].0 then
ADecimalTotal:= ADecimalTotal+1;
End_if;
if AHex[Ai].1 then
ADecimalTotal:= ADecimalTotal+2;
End_if;
if AHex[Ai].2 then
ADecimalTotal:= ADecimalTotal+4;
End_if;
if AHex[Ai].3 then
ADecimalTotal:= ADecimalTotal+8;
End_if;
if AHex[Ai].4 then
ADecimalTotal:= ADecimalTotal+16;
End_if;
if AHex[Ai].5 then
ADecimalTotal:= ADecimalTotal+32;
End_if;
if AHex[Ai].6 then
ADecimalTotal:= ADecimalTotal+64;
End_if;
if AHex[Ai].7 then
ADecimalTotal:= ADecimalTotal+128;
End_if;
ADecimal[Ai]:= ADecimalTotal;
//Change Decimal Array (ADecimal) back to Hex and
//put it in a Decimal Array of Hex Values (AHexInDec)
AHexTemp1:=ADecimalTotal / 16;
AMSDHex:= AHexTemp1 * 16;
ALSDHex:= ADecimalTotal - AMSDHex;
AHexInDec[Ai]:= AHexTemp1*10 + ALSDHex;
ADecimalTotal:=0;
End_for;
AnOutputTotalInt:= (AHexInDec[0]*1)+(AHexInDec[1]*100)+(AHexInDec[2]*10000)+(AHexInDec[3]*1000000);
End_if;
 
Last edited:
Once again Daba thanks for your insight and help.
I did end up using the BTD and FRD instructions for Application two and trashed the Structured Text routine (it did work it was just to hard to follow). Thanks for that!

For application 2 I did make UTDs and AOIs to do all the "work" so when we start moving all the racks to Logix5000 it will be simple and uniform.
If anyone is interested I could post (or send) the UDTs and AOIs.

Once again, Thanks Daba for the schooling! 👨🏻‍🏫 :geek:
 
Once again Daba thanks for your insight and help.
I did end up using the BTD and FRD instructions for Application two and trashed the Structured Text routine (it did work it was just to hard to follow). Thanks for that!

For application 2 I did make UTDs and AOIs to do all the "work" so when we start moving all the racks to Logix5000 it will be simple and uniform.
If anyone is interested I could post (or send) the UDTs and AOIs.

Once again, Thanks Daba for the schooling! 👨🏻‍🏫 :geek:

You're welcome, and thanks for the acknowledgement (y)
 

Similar Topics

Hi I need to convert a DINT with HEX value e.g A0F15663 to a string with the same value 'A0F15663'. Any tips of good instructions to use ...
Replies
11
Views
3,426
I actually solved this, but I thought this info may help others. Condition which prompted this need: I was using a function provided to me by...
Replies
13
Views
3,762
Hey All, i have been fortunate enough to play around with an Applied Motion Products Servo Drive SV200 with 100 watt/ 24vdc congif. I managed...
Replies
2
Views
1,897
I'm trying to display a Allen Bradley Controller Serial Number that's in HEX to String for my SCADA to display. Any ideas?
Replies
3
Views
2,874
Hi All. I have a PLC connected to citect by modbus.I need to transfer a value in HEX from the PLC to the Citect, and to convert it to Time value...
Replies
12
Views
4,315
Back
Top Bottom