LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

DBL to Little Endian Data Types

I am working on type casting a DBL to U16 and U32 and then manipulating the data to Little Endian.  Can LabVIEW community help verify which of the following is correct?  I've successfully confused myself and now I'm not confident on the answer.  "Any help here would be hot."  (Kudos to anyone who knows who says that)

 

Note: The differences are word swap, byte swap, U32 and U16, and Reverse 1D Array.Little Endian1.JPGLittle Endian2.JPGLittle Endian3.JPGLittle Endian4.JPG

0 Kudos
Message 1 of 16
(5,365 Views)

A simple solution would be to use Flatten To String and Unflatten From String.  There are inputs for the Endianness.

 

 

 

Do you have an example of an input and what the expected output should be?  Is this going to another system or to something else that you are developing?  If the later, then it should not really matter as long as you are consistant.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 2 of 16
(5,354 Views)

Crossrulz beat me to the Flatten/Unflatten approach, but let me elaborate a bit on the code you show. First, in all of the cases... the typecast to an array of boolean probably doesn't do what you expect, if you think you're going to get each individual bit. A boolean is a byte in LabVIEW - 0 is false, any other value is true - so you're hiding a lot of information with the conversion to bool.

 

Let's pretend that your DBL is the 8 bytes ABCDEFGH. What you appear to be doing is trying to reverse those to get HGFEDCBA. Here's what each of your attempts is actually producing:

 

1) After the reverse 1-D array you'll have GHEFCDAB. Swap Words has no effect on 16-bit values so that will be your final result.

2) After the reverse 1-D array you'll have GHEFCDAB. Swap Bytes will then produce the desired HGFEDCBA.

3) After the reverse 1-D array you'll have EFGHABCD. Swap Words will then produce the incorrect GHEFCDAB.

4) After Swap Words you'll have CDABGHEF, and after Swap Bytes you'll have DCBAHGFE.

 

Another approach would be a typecast to U8, followed by a reverse 1D array. But flatten or typecast to a string, followed by unflatten with the endianness set, is easy and clear.

Message 3 of 16
(5,338 Views)
Message 4 of 16
(5,325 Views)

I've attached 3 images comparing Crossrulz' example to the above solutions.

 

nathand, this is why I have successfully confused myself.  The answers are not the same.  Thank you, though, for explanation.  Greatly appreciated.  Any chance y ou can help clear up why solutions are not the same for the #2 comparison?

0 Kudos
Message 5 of 16
(5,317 Views)

@nathand wrote:

the typecast to an array of boolean probably doesn't do what you expect, if you think you're going to get each individual bit. A boolean is a byte in LabVIEW - 0 is false, any other value is true - so you're hiding a lot of information with the conversion to bool.


Yeah, I'm not sure what the Boolean array is for.  I'll look into that later.  This is old Legacy code and I'm fixing issues here.

0 Kudos
Message 6 of 16
(5,302 Views)

@DailyDose wrote:

I've attached 3 images comparing Crossrulz' example to the above solutions.

 

nathand, this is why I have successfully confused myself.  The answers are not the same.  Thank you, though, for explanation.  Greatly appreciated.  Any chance y ou can help clear up why solutions are not the same for the #2 comparison?


I'm not quite sure what you're asking, but I see the problem with Crossrulz's solution. What you really want to do is flatten to a string, then unflatten to a 64-bit scalar with the endianness set, then typecast that to an array of U16 or whatever. At that point a typecast to a U8, followed by a reverse array, then typecast back to U16 might be just as easy.

 

Let's look at what happens in the flatten/unflatten case, again assuming you start with ABCDEFGH. The initial flatten to string doesn't change anything. The unflatten says we're working with 16-bit data types, so it grabs the first two bytes, unflattens those with the opposite endianness (to BA), then moves on to the next two bytes (DC), etc. However, you actually wanted to change the endianness of the entire 64-bit (8-byte) value. If you were to unflatten to a 64-bit type, the unflatten function would correctly grab 8 bytes and reverse the order.

0 Kudos
Message 7 of 16
(5,277 Views)

@nathand wrote:

What you really want to do is flatten to a string, then unflatten to a 64-bit scalar with the endianness set, then typecast that to an array of U16 or whatever. At that point a typecast to a U8, followed by a reverse array, then typecast back to U16 might be just as easy.


This seems like a bit much.  Sounds like the Reverse 1D Array and byte swap will suffice for a U16.  But what about in the case of a 32 bit?  Both of the two examples I originally posted sound like they will not work.

0 Kudos
Message 8 of 16
(5,262 Views)

Remember to what Big Endian and Little Endian refer -- the order of the bytes representing the quantity.  Thus if you want to do an Endian conversion yourself, typecast Whatever (Dbl, U64, Extended) to an array of U8 (a.k.a. an Array of Bytes), reverse the array, and typecast back to Whatever.

 

Bob Schor

Message 9 of 16
(5,251 Views)

@Bob_Schor wrote:

Remember to what Big Endian and Little Endian refer -- the order of the bytes representing the quantity.  Thus if you want to do an Endian conversion yourself, typecast Whatever (Dbl, U64, Extended) to an array of U8 (a.k.a. an Array of Bytes), reverse the array, and typecast back to Whatever.

 

Bob Schor


Shouldn't I reverse the array and do the byte swap? 

0 Kudos
Message 10 of 16
(5,243 Views)