# Write Opc Tag Hex from concat string

**URL:** <https://forum.inductiveautomation.com/t/write-opc-tag-hex-from-concat-string/20756>\
**Category:** Ignition\
**Created:** [November 12, 2018, 8:16am UTC](https://forum.inductiveautomation.com/t/write-opc-tag-hex-from-concat-string/20756 "2018-11-12T08:16:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![claudio.calza](https://avatars.discourse-cdn.com/v4/letter/c/a698b9/32.png) [@claudio.calza](https://forum.inductiveautomation.com/u/claudio.calza)\
**Post date:** [November 12, 2018, 8:16am UTC](https://forum.inductiveautomation.com/t/write-opc-tag-hex-from-concat-string/20756/1 "2018-11-12T08:16:45Z")

</div>

Hi I read 14 opc tags in Hex that make up the article name. I get the string through  
runscript - dec2str (DecVal):  
s = str (hex (DecVal))  
c1 = s (2: 4)  
c2 = s (4: 6)  
return chr (int (c2,16)) + chr (int (c1,16)),  
with derived Tags, after concatenate the tags - concat conTag1 + conTag2, etc … and I get my string. Now I have to edit the string and write the article name on the opc tags by converting the string into 14 hex tags. I have 14 tags with value int  
example:  
opc tag1 = 19267  
opc tag2 = 18012  
conTag1 = CK  
conTag2 = \ F  
my string concat =" CK \ F"  
If I modify the string how do I write the int value in opcTag?  
Sorry I’m a beginner with ignition and, thanks to the forum and to the manual and the inductive university I managed to read tags and convert the hex value to chr and concatenate the values ​​in string but I can not divide and convert the string of 40 characters in integers and write on the opc tags. I tried split - ord - list - but I can not find the way to write the value int in the opc tag - could you help me? thank you

---

<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 12, 2018, 4:20pm UTC](https://forum.inductiveautomation.com/t/write-opc-tag-hex-from-concat-string/20756/2 "2018-11-12T16:20:28Z")

</div>

Hi Claudio, welcome to the forums! 🙂

Consider using a script to read/write all your tags at once and make a string out of them. Jython has access to both python and Java libraries, so there’s a lot of built-in things that can make life easier for you. In this instance java has libraries to help with byte swapping. We’ll use the Short library, because in Java, a Short is a 16-bit integer.

```python
#----------------------------------------------
# tags to string
#----------------------------------------------

import binascii
import java.lang.Short as Short

# list of tags containing your string
tags = ['[Client]Tag1','[Client]Tag2']

# read the tag values
tagIn = system.tag.readAll(tags)

# create hex string of the tag values
hexIn = ''
for tag in tagIn:
  # byte swapping.
  value = Short.reverseBytes(tag.value)
  # concatenate hexvalues (minus the '0x')
  hexIn += hex(value)[2:]

# print the result string
print hexIn.decode('hex')

```

Going the other way is essentially reversing the procedure. The tricky part, as you’ve already found out, is how to separate things into 2-byte chunks. I’ll admit to having used Google to figure that one out!

```python
#----------------------------------------------
# string to tags
#----------------------------------------------

import binascii
import java.lang.Short as Short

# list of tags containing your string
tags = ['[Client]Tag1','[Client]Tag2']

# string to write
stringOut = 'CD\F'

# convert the string to a hex string
hexIn = binascii.hexlify(stringOut)

# break into a list of two-byte hex values. It just groups into characters of 4, 
# joined by a comma, then split again to make a list. Thanks Google!
hexOut = (','.join(hexIn[i: i+4] for i in range(0, len(hexIn), 4))).split(',')

# create list of integers to write to the tags
valuesOut = []
for i in hexOut:
  # byte swapping
  valuesOut.append(Short.reverseBytes(int(i,16)))

#write to tags
system.tag.writeAll(tags, valuesOut)

```

Hope this helps!

---

<div class="post-metadata">

**Author:** ![claudio.calza](https://avatars.discourse-cdn.com/v4/letter/c/a698b9/32.png) [@claudio.calza](https://forum.inductiveautomation.com/u/claudio.calza)\
**Post date:** [November 12, 2018, 5:24pm UTC](https://forum.inductiveautomation.com/t/write-opc-tag-hex-from-concat-string/20756/3 "2018-11-12T17:24:28Z")

</div>

Thanks a lot Jordan I read your many solutions, I tried to use the functions, but I have not yet solved, in “string to tags” I get an error  
“output = binascii.a2b\_hex (input)”  
" TypeError: 0dd-Lenght string"  
do you have any idea what the error is?  
Thank you very much

---

<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 12, 2018, 5:47pm UTC](https://forum.inductiveautomation.com/t/write-opc-tag-hex-from-concat-string/20756/4 "2018-11-12T17:47:00Z")

</div>

Is there anything extra in the string, like a new line or whitespace?

---

<div class="post-metadata">

**Author:** ![claudio.calza](https://avatars.discourse-cdn.com/v4/letter/c/a698b9/32.png) [@claudio.calza](https://forum.inductiveautomation.com/u/claudio.calza)\
**Post date:** [November 12, 2018, 6:58pm UTC](https://forum.inductiveautomation.com/t/write-opc-tag-hex-from-concat-string/20756/5 "2018-11-12T18:58:15Z")

</div>

Thanks Jordan is true are the special characters, if I try with normal characters extracts the string. While the “tags to string” does not write in the tags and does not show errors …

---

<div class="post-metadata">

**Author:** ![claudio.calza](https://avatars.discourse-cdn.com/v4/letter/c/a698b9/32.png) [@claudio.calza](https://forum.inductiveautomation.com/u/claudio.calza)\
**Post date:** [November 12, 2018, 7:02pm UTC](https://forum.inductiveautomation.com/t/write-opc-tag-hex-from-concat-string/20756/6 "2018-11-12T19:02:20Z")

</div>

OK , the “tags to string” write in the tags … you’re really good, I have the problem of special characters  
Thank

---

<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 12, 2018, 7:22pm UTC](https://forum.inductiveautomation.com/t/write-opc-tag-hex-from-concat-string/20756/7 "2018-11-12T19:22:16Z")

</div>

Glad you got it sorted out. 🙂
