LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

byte to string help

Hello,

I am using the attached vi to convert a hex value comprised of 2 bytes of data to a string value.  I have run into an issue with the string being truncated to remove leading zeroes from the hex bytes, this is important because the bytes are swapped.  Let me illustrate the problem with an example:

In Theory:  I start with 0B FF, the bytes are swapped and should give FF0B, this is turned into a string and should give the decimal number -245.
In Practice:  I start with 0B FF, the bytes are swapped and give 0FFB (this leading zero is inserted to show the 2 bytes), this is turned into a string and gives the decimal number 4091.

Any suggestions on how to preserve the leading zero? 

Cheers,
Jon
0 Kudos
Message 1 of 10
(11,689 Views)
I figured it out, just needed to set the min width of the hex string to preserve leading zeroes.  Should have spent a few more minutes on it first...
0 Kudos
Message 2 of 10
(11,677 Views)

Wire up a constant of 2 to the first function.

Message Edited by Ravens Fan on 09-21-2007 01:15 PM

0 Kudos
Message 3 of 10
(11,677 Views)

Simply reverse the array and typecast it to a I16. Voila! 🙂

There is no need to use anything pink! 😮

Message Edited by altenbach on 09-21-2007 10:30 AM

Message 4 of 10
(11,671 Views)
As a side point, there are easier ways to concatenate an array of strings. See my latest entry in the Rube Goldberg thread.

😄

0 Kudos
Message 5 of 10
(11,649 Views)
reversing the array only works with one value (admittedly that was the posed problem). But if you cast to I16 first, and then use Swap Bytes, you can convert an array of any size.
Message 6 of 10
(11,627 Views)


@jdunham wrote:
reversing the array only works with one value (admittedly that was the posed problem). But if you cast to I16 first, and then use Swap Bytes, you can convert an array of any size.

Sure, each new situation has its own optimized solution. For example if we had 4  bytes in an U8 array (little endian) and want an I32, you can no longer simply swap, but need a few more steps.

I always wondered why LabVIEW does not have a "reverse bytes" primitive in the "data manipulation" palette that would do it for any size numeric in one step (swap endian).

(Now JK will probably tell me that openg has one. 🐵

0 Kudos
Message 7 of 10
(11,619 Views)


@altenbach wrote:

I always wondered why LabVIEW does not have a "reverse bytes" primitive in the "data manipulation" palette that would do it for any size numeric in one step (swap endian).

(Now JK will probably tell me that openg has one. 🐵



Not that I'm aware of, Christian.  Want to write one?

PS - Yes, the walls do have ears 😉

Message Edited by Jim Kring on 09-21-2007 07:15 PM

0 Kudos
Message 8 of 10
(11,611 Views)


@altenbach wrote:

Sure, each new situation has its own optimized solution. For example if we had 4  bytes in an U8 array (little endian) and want an I32, you can no longer simply swap, but need a few more steps.


Right. you need two simple swaps (byte and word).



@altenbach wrote:

I always wondered why LabVIEW does not have a "reverse bytes" primitive in the "data manipulation" palette that would do it for any size numeric in one step (swap endian).



Well I had been pushing for NI to add that to the type cast function, but instead they finally added it to Flatten to String.  The type cast is crazy because it reverses the endianness whether you want it to or not.
0 Kudos
Message 9 of 10
(11,607 Views)


@Jim Kring wrote:
Want to write one?

Writing a simple one would be trivial, but what we really need Is something that handles any input (I64, SGL, U16, etc. as well as arrays and clusters of such in any topology and mix). This exceeds my programming skills. 🙂

0 Kudos
Message 10 of 10
(11,586 Views)