# С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:** 12\
**Page:** 2

<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 29, 2011, 5:18pm UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/21 "2011-11-29T17:18:54Z")

</div>

Good day!  
We came another question.  
Our function in the element is invoked as follows

```auto
runScript(concat("app.DMK.convert(",toStr({Power Supply /DMK32 First Power input/Active power of phase L1}),")"))
```

We need to return the element itself Decrease the number of such in 1000, as it can be done without making addenda in the function.

can be passed to the function parameter More like so

`def convert(inputValue, inputParam):
 if inputParam == 1:
outputValue=int(str((hex(inputValue))[:-4]),16)/1000
else
outputValue=int(str((hex(inputValue))[:-4]),16)
return outputValue`  
But unfortunately, this design does not work  
Another problem is the value of type int, because if for example 21300 / 1000 = 21.3, and the function returns 21

---

<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 29, 2011, 7:11pm UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/22 "2011-11-29T19:11:22Z")

</div>

The problem is solved!  
Function

`def convert(inputValue,inputParam):
   return float(int(str((hex(inputValue))[:-4]),16)) / inputParam
`  
The function call

```auto
runScript(concat("app.DMK.convert(",toStr({inputValue}),",1000",")"))
```

---

<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 29, 2011, 7:35pm UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/23 "2011-11-29T19:35:33Z")

</div>

[Fix]  
Function

`def convert(inputValue,inputParam):
   return float(inputValue >> 16) / inputParam`  
The function call

```auto
runScript(concat("app.DMK.convert(",toStr({inputValue}), ",", toStr(1000),")"))
```

---

<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 29, 2011, 10:19pm UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/24 "2011-11-29T22:19:01Z")

</div>

After you define the function as described in the previous post, periodically get an error:

[code]Exception: Error executing expression binding on  
First power input.Root Container.Power.Rated power .Rated power of phase L1.value  
caused by ExpressionException: Error parsing script for runScript() expression: Traceback (innermost last):  
(no code object) at line 0  
OverflowError: integer literal too large

```
caused by Traceback (innermost last):

```

(no code object) at line 0

OverflowError: integer literal too large

Ignition v7.3.1 (b496)  
Java: Sun Microsystems Inc. 1.6.0\_24[/code]

In what may be the problem?

---

<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 30, 2011, 12:04pm UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/25 "2011-11-30T12:04:20Z")

</div>

Sorry, I was on vacation yesterday. Let me look over what you’ve got, and I’ll post something up for you. 🙂

---

<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 30, 2011, 12:24pm UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/26 "2011-11-30T12:24:17Z")

</div>

I modified the function to change the order of magnitude as a function of the transmitted Parameters. as follows

`def convert(inputValue,inputParam):
   return float(inputValue >> 16) / inputParam`

The function call

```auto
runScript(concat("app.DMK.convert(",toStr({inputValue}), ",", toStr(1000),")"))
```

The converted value is divided by 1000  
This method works, but occasionally an error

```auto

Exception: Error executing expression binding on
First power input.Root Container.Power.Rated power .Rated power of phase L1.value
   caused by ExpressionException: Error parsing script for runScript() expression: Traceback (innermost last):
  (no code object) at line 0
OverflowError: integer literal too large

   caused by Traceback (innermost last):

  (no code object) at line 0

OverflowError: integer literal too large

Ignition v7.3.1 (b496)
Java: Sun Microsystems Inc. 1.6.0_24
```

In what may be the problem?

---

<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 30, 2011, 1:44pm UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/27 "2011-11-30T13:44:57Z")

</div>

Try:

`def convert(inputValue,inputParam):
   return float(1L*inputValue >> 16) / inputParam`

I think that occasionally it attempting to use the inputValue as an integer when it should be using it as a long integer. Multiplying by 1L should coerce the input to a long.

Nice use of a bit shift, by the way! 😃

---

<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 30, 2011, 3:06pm UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/28 "2011-11-30T15:06:05Z")

</div>

Unfortunately did not help

---

<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 30, 2011, 3:28pm UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/29 "2011-11-30T15:28:28Z")

</div>

All right, how about the previous one?

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

Or if looks too complex-- Not too complex for you to understand, just if it looks too busy:

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

Also, do you have an example of what input value does not work?

---

<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 30, 2011, 6:00pm UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/30 "2011-11-30T18:00:58Z")

</div>

Managed to get rid of the error.  
currently function looks like:

`def convert(inputValue,inputParam):
   return float(long(inputValue) >> 16) /inputParam
`  
Error stopped appearing after SalTag type has been changed to Float.

P.S.  
Could you show a small example of how to use the script in the transation group to save data base the converted values.  
🙂

---

<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 30, 2011, 6:05pm UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/31 "2011-11-30T18:05:56Z")

</div>

Sure! I have a meeting to run off to, but I’ll put one together afterwords.

---

<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 30, 2011, 8:06pm UTC](https://forum.inductiveautomation.com/t/onverting-values/3632/32 "2011-11-30T20:06:53Z")

</div>

Here you go.

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

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