AB - Convert String (Hex Format) to Decimal

lotuseater

Member
Join Date
Nov 2005
Location
MO
Posts
3
We are doing some testing communicating serially with a linear actuator in order to get its current position using an Allen Bradley Compact Logix processor. The information comes back as a string and I am able to extract the part of the string I need using an MID instruction.
This string contains a hexadecimal value. The actuator company has demonstrated to me how find the actual position using this value within Microsoft Excel, but I would like some assistance automating this in my PLC.

Here is an example of how to find the position using Excel:
String Value from device: 'FFFFEC14'
Excel HEX2DEC() = 4294962196

Constant Base Position: 'FFFFFFFF'
Excel HEX2DEC() = 4294967295

Actual Position = 4294962196 - 4294967295 - 1 = -5100
Actual Position is -51.00 mm

This matches up to the actual value when looking at the front end software for the device, but I would like to complete this procedure within my PLC.

Does anyone have any tips for converting the string value (HEX) to the appropriate decimal value?

Thanks.
 
Hi,

Welcome to the forum,

Create a tag, say you name it
1. "Source" with Datatype = Dint, Style = Hex

and another tag called
2. "Destination" with Datatype = Dint, Style = Decimal.

Then use a 'Mov' command to move your values from 'Source' to 'Destination' - gets you a converted value.

Regards
_______
 
The problem I see is converting the HEX value which is actually contained in a STRING tag into a DINT decimal value. I don't think you can do this with an instruction. I think you will have to create some logic in a subroutine which will convert each HEX string digit into a HEX DINT and then convert the whole thing from a HEX DINT to a DECIMAL DINT.
 
You might do better in structured text than ladder.

Look at each character in the string right to left in a loop.

Get the ASCII code which should be easy then subtract 48 (decimal) which is the ASCII code for 0.

Then multiply the number by 16^n where n is the place value your loop is currently in and add this to a running total.

Make sure you are summing into an unsigned double integer type.
 
Like mentioned, one of the problems is that my hex digits are actually laid out within a string tag. Each digit within the string tag is in an ASCII format instead of hex, but this can be accounted for at ndzied1 mentioned.

My other problem is that these large values require a LINT tag to be used. It looks like most instructions don't like the LINT tag either.
 
Get the ASCII code which should be easy then subtract 48 (decimal) which is the ASCII code for 0.

Assuming the input is HexASCII (the characters '0' through '9' and the characters 'A' througn 'F') then add this step between the two quoted - If number > 9 then subtract 7

Then multiply the number by 16^n where n is the place value your loop is currently in and add this to a running total.
 
This seemed to be useful so I wrote a subroutine to do it. Unfortunately the max hex value it can handle is 7FFFFFFF, or 2147483647, so your solution may lie elsewhere. Hopefully you can use this as a starting point.

I used V16 of Logix5k, hopefully this won't be a problem for people.
 
pandersen said:
This seemed to be useful so I wrote a subroutine to do it. Unfortunately the max hex value it can handle is 7FFFFFFF, or 2147483647, so your solution may lie elsewhere. Hopefully you can use this as a starting point.

I used V16 of Logix5k, hopefully this won't be a problem for people.

That worked beautifully. I had been working on it as well and came up with a similar solution, but your's is MUCH cleaner with that JMP loop in there. Also, the CPT instruction instead of the BSLs and MVMs I had is much cleaner.
The range of the numbers is acceptable as well. I feel stupid for not noticing at first, but all the excel steps they had me do is not needed for a 32-bit Hex number. The first 'F' signifies a negative in the PLC, which directly translates to the -5100 in the example in the first post when taken from Hex to Dec style in the PLC.
Thank You!
 
Last edited:
Opposite Problem

Hi, I've been around this forum, but don't generally post questions.
I know it's been a while since this last post, but I basically have the same problem, but in the opposite direction.
I need to take a DINT value, and represent it as HEX as the first 4 characters of a string. I tried using a DTOS instruction, but for example if my Dint is 244, i end up getting "244" as my string, and not "00F4" which is what a I really need.

edit - I wound up finding my answer at: http://www.plctalk.net/qanda/showthread.php?t=78380 In case someone else is looking for it too!
 
Last edited:

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,401
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,855
First post here, hopefully not breaking any rules. I am working with a Redlion G3 HMI (yes, I know). I have a series of string tags that I am...
Replies
17
Views
4,071
hi, i try to capture barcode data using UDT with SINT ascii array. i have all the data i need but it in array format, how can i convert to 1...
Replies
5
Views
3,059
Found an awesome point of sale for $500. It sends an ascii string when a product is sold. Need to convert this into a number between 0 and 255 or...
Replies
1
Views
2,274
Back
Top Bottom