Dev Space

Customize & extend the power of Alteryx. SDKs, APIs, custom tools, and more!
It's the most wonderful time of the year - Santalytics 2020 is here! This year, Santa's workshop needs the help of the Alteryx Community to help get back on track, so head over to the Group Hub for all the info to get started!

Python Tool to Translate Hexidecimal to Unicode


Hi Developers!


I saw this post in the Alteryx Designer Discussions, and although Alteryx does support Unicode encoding by default, it does not have a native way of translating other encodings into Unicode. This got me wondering if there might be a way to leverage the Python SDK for this task. I was able to modify the Python Single Input Output Example Tool to accomplish this task. The core of the code is a function that translates hexadecimal to Unicode.


def unicodeTrans(msg):
	new_msg = []
	for char in msg:
			char = chr(ord(char))
		except ValueError:
			char = '?'
	return ''.join(new_msg)


This function was working really well when ran in an IDE, but was failing to execute the translation in the Python SDK Tool. It turned out this was because of the way values are passed through the Alteryx Engine to Python. The Python SDK uses UTF-8, and the input values are passed in from the engine as strings. This required that I convert the strings to bytes before passing them into the translation function, in order for the function to work properly. 


bts = bytes(text, 'utf-8').decode("unicode_escape")
result = unicodeTrans(bts)


This is a pretty simple tool, but I had a lot of fun with this exercise learning more about the Python SDK!





Really cool use case @SydneyF! Thanks so much for sharing 

16 - Nebula
16 - Nebula

Thank you for posting this @SydneyF  - the documentation for the SDK is still a little rough, so having examples like this to work through really helps.

The other SDK examples are out there - but I couldn't find one that added a simple calculated field (which is what I'm trying to achieve, and which yours does).