From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Most efficient hex string to number conversion

Hi All,
  I am working on command and control software for a scientific instrument that loves to return data in hex, little endian, 4 byte format.  That is, 0500 0000 should be converted to 5 where 5 is in U32 format, 0A01 0000 should return 266, and 0854 AC4F should return 1,336,693,768.  I've cludged something together that works, but the problem is that it does not work fast enough.  I've attached my code, which does work under Labivew 6.1 and windows XP.  Does anyone have any suggestions to make the code run faster?  Even incremental increases would help.  Thanks,
 
PP
0 Kudos
Message 1 of 22
(4,148 Views)
Assuming your string is 4 bytes, convert it to a byte array (String to byte array), reverse the array (reverse 1D array), then cast it to U32 (typecast). See attached image.
 

Message Edited by altenbach on 08-08-2005 11:36 AM

Message 2 of 22
(4,125 Views)

The key is the Join Numbers VI in the Advanced>>Data Manipulation palette.

Use Index Array on the byte array. Join elements 1 and 0 and elements 3 and 2, then join the results and you have your U32. Note that because of the endianess, the bottom number has to go into the hi byte (that was why I said 1 and 0 and not 0 and 1).

Edit: As usual, Altenbach's code is cooler (and probably more efficient. You can bench them if you like).

Message Edited by tst on 08-08-2005 09:40 PM


___________________
Try to take over the world!
Message 3 of 22
(4,120 Views)
Try this:
- tbob

Inventor of the WORM Global
Message 4 of 22
(4,116 Views)

Wow,

  That's great!  I've improved my perfermance by 4.5 times!  Thanks to all, and yes, as you suspected tst, Altenbach is a little more efficient, but thanks anyways.  As for tbob, thanks, but I can't view it.  I'm still using 6.1.  Thanks guys,

PP

0 Kudos
Message 5 of 22
(4,111 Views)


@Perl Peril wrote:

Wow,

  That's great!  I've improved my perfermance by 4.5 times!  Thanks to all, and yes, as you suspected tst, Altenbach is a little more efficient, but thanks anyways.  As for tbob, thanks, but I can't view it.  I'm still using 6.1.  Thanks guys,

PP



I also couldn't read tbob's VI. Probably it's the same I would suggest: simply use 'Reverse String' and then type cast, it is 5-6 times faster than altenbach's

(BTW: I forgot how to include the picture into the post, so it's just attached)
Message 6 of 22
(4,101 Views)
Altenbach's might be slightly faster, but I wouldn't want you to miss out on mine:
 
 
- tbob

Inventor of the WORM Global
0 Kudos
Message 7 of 22
(4,099 Views)

Actually, my second idea was the similar to tbob's and I just finished a few benchmarks with something very similar (great minds think alike ;)).  It seems to be faster than my original draft. On my rig, it is about 100x faster than the original by Perl Peril. (80ms vs 8000ms for 1 million conversion in a loop). 

(On the same benchmark, fahlers VI takes about 600ms. I don't know why it is slower, it looks pretty elegant, too :))

Please test for yourself. 🙂

Message Edited by altenbach on 08-08-2005 12:01 PM

Message Edited by altenbach on 08-08-2005 12:11 PM

Message 8 of 22
(4,074 Views)

I've implemented tst's modified version of altenbach's code and did indeed find it to be about 3x faster that altenbach's original. But I've never seen the symbol in Altenbach and tbob's code. What does it do? Is it only in labview 7? (Again, I'm on 6.1). Thanks.

0 Kudos
Message 9 of 22
(4,063 Views)

SOME ORDER, PLEASE!!!!

tbob's and Altenbach's second solution are identical to mine, so you can read my post.

The optimized code was not mine but Franz's (Way to go, Franz, 5 stars).


___________________
Try to take over the world!
0 Kudos
Message 10 of 22
(4,053 Views)