# Сonverting values

**URL:** <https://forum.inductiveautomation.com/t/onverting-values/3632>\
**Category:** Ignition\
**Created:** [November 21, 2011, 9:50am UTC](https://forum.inductiveautomation.com/t/onverting-values/3632 "2011-11-21T09:50:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![akonovalov](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@akonovalov](https://forum.inductiveautomation.com/u/akonovalov)\
**Post date:** [November 21, 2011, 9:50am UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/1 "2011-11-21T09:50:26Z")

</div>

Good afternoon!  
From the OPC server, we get a value, but in order to display it in the correct format in Ignition we need to convert it to the following algorithm:

DEC\_VALUE convert HEX\_VALUE  
HEX/10000  
HEX\_VALUE convert DEC\_VALUE  
DEC\_VALUE - The converted value

Example:  
26214400(DEC) convert 1900000(HEX)  
1900000/10000  
190(HEX) convert 400(DEC)  
400 - Voltage

Could you suggest how to implement this algorithm in Ignition.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 21, 2011, 10:23am UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/2 "2011-11-21T10:23:41Z")

</div>

in a script:

```auto
int("0x"+str(int(str(hex(26214400))[2:])/10000),16)
```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 21, 2011, 10:55am UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/3 "2011-11-21T10:55:45Z")

</div>

Ok, I’ll post a full solution in a moment. It’ll take a bit to type, so stay tuned…

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 21, 2011, 11:00am UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/4 "2011-11-21T11:00:30Z")

</div>

I’d have been quicker at getting this to you, but had a line call to take care of.

I added a script module called voltage. In the module I defined a function called convert

`def convert(inputValue):
	return int("0x"+str(int(str(hex(inputValue))[2:])/10000),16)`

Then in the Numeric label, or whatever you’re using to display the number, bind the value to an expression similar to this:

```auto
runScript(concat("app.voltage.convert(",toStr({RawValue}),")"))
```

The {RawValue} is replaced by whatever OPC tag you are using.

Hope this helps!

EDIT: here’s the module you may import, if you wish…  
EDIT#2: replace bad code with working one from below conversation.  
[voltage.py](https://us1.discourse-cdn.com/inductiveautomation/original/1X/b961613d7c13afcc9c18861596043f0f58e5250d.py) (66 Bytes)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 21, 2011, 11:18am UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/5 "2011-11-21T11:18:47Z")

</div>

Pictures! Worth a thousand words a piece! Bid on ebay! ![:mrgreen:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/d23659b203d2d24d394be983a52adb2fa6fe23d4.gif "Mr. Green")

The Module Script…

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/1X/49b1d8a5985b66fe2755fff7f7ed9845e3f63467.png)  

…the Numeric Label…

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/1X/f752f1e5cfd135876810039e79596624771aaa45.png)  

…and binding it’s value property.

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/1X/ec32f979421b397459275e92d3dd02dfa102e8a2.png)

---

<div class="post-metadata">

**Author:** ![akonovalov](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@akonovalov](https://forum.inductiveautomation.com/u/akonovalov)\
**Post date:** [November 22, 2011, 7:59am UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/6 "2011-11-22T07:59:45Z")

</div>

I did everything as you said

1. Create SQL Tag L1 Phase voltage  
2. Creat Python script - DMK  
3. Create Num Label and in the field to have an expression with the argument in the form of the required tag  
  
But the script fails and an error

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 22, 2011, 10:18am UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/7 "2011-11-22T10:18:29Z")

</div>

Sorry about that. Looks like I need a little more information. ![:scratch:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/76d1620bec252bfac07050c889ff5a91dc8a9c25.gif ":scratch:")

Are these true hex values you are working with? are we really dividing by 10000 or just removing the end four zeroes?

The example you gave ended up with a nice BCD-looking number. This time, though it converted to the hex value e00000, which isn’t divisible by 10000.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 22, 2011, 10:34am UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/8 "2011-11-22T10:34:53Z")

</div>

Ok, I took a closer look at your original post. If I had been thinking a bit more clearly (while it is afternoon for you, it’s early morning for me, and I hadn’t had any coffee yet 😆 ), I would have noticed earlier what you were looking to do!

try replacing the function code with this:

`def convert(inputValue):
	return int("0x"+str((hex(inputValue))[2:-4]),16)`

Edited for function input

---

<div class="post-metadata">

**Author:** ![akonovalov](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@akonovalov](https://forum.inductiveautomation.com/u/akonovalov)\
**Post date:** [November 22, 2011, 10:35am UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/9 "2011-11-22T10:35:20Z")

</div>

We removing the end four zeroes. And translate back into view demyatichny

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 22, 2011, 10:44am UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/10 "2011-11-22T10:44:39Z")

</div>

Ok, that’s what I thought.[quote=“JordanCClark”]

Try replacing the function code with this:

`def convert(inputValue):
   return int(str((hex(inputValue))[:-4]),16)`

EDIT: streamlined it from previous post

---

<div class="post-metadata">

**Author:** ![akonovalov](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@akonovalov](https://forum.inductiveautomation.com/u/akonovalov)\
**Post date:** [November 22, 2011, 10:52am UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/11 "2011-11-22T10:52:42Z")

</div>

Unfortunately the code is not fulfilled

```auto
def convert(inputValue):
   return int(int(str((hex(14680064))[:-4]),16)
```

Error

```auto
Traceback (innermost last):
  (no code object) at line 0
SyntaxError: ('invalid syntax', ('<string>', 3, 1, ''))
```

The code from the previous post is working correctly

```auto
def convert(inputValue):
   return int("0x"+str((hex(14680064))[2:-4]),16)
```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 22, 2011, 10:54am UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/12 "2011-11-22T10:54:52Z")

</div>

Sorry, there was an extra int function that has been corrected. either will work ok now. 😃

My coffee is still trying to start my brain…

---

<div class="post-metadata">

**Author:** ![akonovalov](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@akonovalov](https://forum.inductiveautomation.com/u/akonovalov)\
**Post date:** [November 22, 2011, 11:00am UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/13 "2011-11-22T11:00:32Z")

</div>

This function works correctly, thank you very much for your help, I will continue to improve their knowledge of Python.

```auto
def convert(inputValue):
   return int("0x"+str((hex(14680064))[2:-4]),16)
```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 22, 2011, 11:06am UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/14 "2011-11-22T11:06:46Z")

</div>

No problem. I’m just sorry I had a few missteps in there for you.

The download for the function has been updated to reflect these changes.

Now it’s time for a coffee refill! 😆

---

<div class="post-metadata">

**Author:** ![akonovalov](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@akonovalov](https://forum.inductiveautomation.com/u/akonovalov)\
**Post date:** [November 22, 2011, 11:18am UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/15 "2011-11-22T11:18:25Z")

</div>

Function now works correctly

```auto
def convert(inputValue):
   return int(str((hex(inputValue))[:-4]),16)
```

---

<div class="post-metadata">

**Author:** ![akonovalov](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@akonovalov](https://forum.inductiveautomation.com/u/akonovalov)\
**Post date:** [November 22, 2011, 7:20pm UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/16 "2011-11-22T19:20:46Z")

</div>

I would like to ask one question.  
How to use this feature to build EasyChart.  
The database value is stored as the number 26214400, and we need to kept in the form of the converted values ​​to the plot of the number to 231 instead of 26214400.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 22, 2011, 8:51pm UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/17 "2011-11-22T20:51:46Z")

</div>

I don’t think you can. You can do it in a regular chart using a query similar to

`select conv(left(hex(Value1),LENGTH(hex(value1))-4),16,10) as voltage1, conv(left(hex(Value2),LENGTH(hex(value2))-4),16,10) as voltage2,...etc...
from table
where ... etc.
`

Another thing you may want to consider is to have your transaction group process the raw data and store it in a useable form into the database. That way you could easily implement the easy chart.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 22, 2011, 8:56pm UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/18 "2011-11-22T20:56:35Z")

</div>

I should have mentioned that the above query was under MySQL.

---

<div class="post-metadata">

**Author:** ![akonovalov](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@akonovalov](https://forum.inductiveautomation.com/u/akonovalov)\
**Post date:** [November 22, 2011, 9:25pm UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/19 "2011-11-22T21:25:57Z")

</div>

The solution to the Transaction group because before we write the variable into the database, we will change it with a written earlier functions.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [November 23, 2011, 10:21am UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/20 "2011-11-23T10:21:19Z")

</div>

Exactly! You can use the runScript function in an expression item to call the scripted function, just like you used earlier in the numeric display. 😃

[Next page](https://forum.inductiveautomation.com/t/onverting-values/3632.md?page=2)
