Example Code

HEX Encoder and Decoder

Code and Documents

Attachment

When sending binary data over the network you need to encode it somehow otherwise bad things happen. One of these bad things is that you loose the high order bit of each byte. That is why things like uuencode, base64, mime and many others exist.

The simplest encoding scheme is to encode each byte as a hexadecimal string. It is not very bandwidth friendly because it exactally doubles the size of the data you want to send.

I use these vi's to send binary data to an Apache mod_python module. I use the Python binascii.unhexlify module to decode back to the original binary data.

The test vi below encodes the LabVIEW executable and then decodes it and verifies that the decoded file is the same as the original. On my Core2Duo 2.8GHz T9600 with 4GB or ram it takes 7.4 seconds to encode 40MB of data and 12 seconds to decode it again.

Feel free to check out the implementation and offer feedback on increasing the performance.

fp.png

Update:

These updates are from a year ago. I just forgot to publish the new version. What is new other than the comments below which I forgot to publish is that I am attaching the files in LabVIEW 8.2 format.

I cut the encode time in half by moving the number to hex string inside the loop. The second diagram executes twice as fast as the first.

bd.pngbd.png

I would still like any comments on how I can improve this even more. I have not yet put any thought into increasing performance of the decode vi since I don't really use it. I don't send binary files back to LabVIEW from the webserver.

=====================
LabVIEW 2012


Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
James_McN
Active Participant Active Participant
Active Participant
on

On the left example, could you replace the for loop with just a flatten to string? You would have to turn off the prepend string/array size but other than that I think it should have the same affect.

James Mc
========
CLA and cRIO Fanatic
My writings on LabVIEW Development are at devs.wiresmithtech.com
jcarmody
Trusted Enthusiast
Trusted Enthusiast
on

Thanks for sharing this.  I may, or may not (depending on your position on IP and software licenses), have used this in an application where I need to zip some data to store in a database that doesn't appreciate the output of the zip utility.

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

PhillipBrooks
Active Participant
Active Participant
on

If your app is Windows only, you can use .NET to compress/uncompress strings:

https://decibel.ni.com/content/docs/DOC-7200#comment-16428


Now is the right time to use %^<%Y-%m-%dT%H:%M:%S%3uZ>T
If you don't hate time zones, you're not a real programmer.

"You are what you don't automate"
Inplaceness is synonymous with insidiousness

SachaTholl
Member
Member
on

Hi,

How should i modify this vi for being able do decode base32hex ?

For example: the base 32hex encoded string looks like this:

XASJJS1A40000AI831LCG003E5001G5H82O0000000000001N

i have found an internet site which allowed me to decode the data first by decoding from base32hex to hex:

(URL: http://tomeko.net/online_tools/base32hex.php?lang=en)

The decoded Decoded data (hexadecimal) looked like this:


57273E054400000549030D5900006E280018162816000000000000000037

Then i used the  Hexadecimal -> decimal series converter from the same site

(exact URL: http://tomeko.net/online_tools/hex_to_dec.php?lang=en)

to finally obtain the desired data . The decoded decimals looked like this (and this this is the content as i need it, its temperature data from several several  sensors respectively)

87,  39,  62,   5,  68,   0,   0,   5,  73,   3,  13,  89,   0,   0, 110,  40,   0,  24,  22,  40,  22,   0,   0,   0,   0,   0,   0,   0,   0,  55

Thank you for helping

Sash

Contributors