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: 

Endian Conversion?

I am importing an NDF data file using labview read file VIs. The header of this file is composed of various strings and I32 type values. The strings read in fine. When attempting to read the I32 type, I am getting incorrect numbers. (I specify I32 type for the input on the Read file fx.) For example, header size should be '2048' but I am getting '524288'. Data Points should be '1024' but I am getting '262144.' I was told I need to convert to little endian format. Byte swapping is trivial for I16/U16 but it is not clear how to attack this with I32 type. Given that we know what the values should be, I was hoping to be able to reverse engineer this somehow. I attempted various byte operations from the labview data manipulation palette with no succ
ess. Any advise here is appreciated.

Thanks,

Don
0 Kudos
Message 1 of 15
(8,569 Views)
Convert the number to boolean array, reverse the array, convert back to number. All are primitive functions and, I think, work in place (meaning they do not create copies) and should be quite fast.

Byte swapping does not reverse the individual bits within each byte.

Lynn
0 Kudos
Message 2 of 15
(8,568 Views)
Hi Don

This VI (takes your number and write them in digital format) will show you what your proble is.

Either you have a 8 bits shift in your readings, either you have to rotate some byte.

As there are several solutions, I am quite shure that with the toll provided you will get it clear

Doc-Doc
Doc-Doc
http://www.machinevision.ch
http://visionindustrielle.ch
Please take time to rate this answer
Message 3 of 15
(8,568 Views)
Hi

You have the solution for I32.
Labview is Big Endian, so perhaps you should take a look at this link to learn how to convert other numeric types from/to any endian
http://digital.ni.com/public.nsf/3efedde4322fef19862567740067f3cc/97332426d63630ee862565070049ffbb?OpenDocument
Hope it helps
Alipio
---------------------------------------------------------
"Qod natura non dat, Salmantica non praestat"
---------------------------------------------------------
0 Kudos
Message 4 of 15
(8,568 Views)
While there may be a more efficient way, I have done the byte
rearrangement by taking the input to "Flatten to String", then "String
to byte array", then "Reverse 1D array", then "byte array to string",
and finally "Unflatten from string" with the type wired to, in my case,
U32. I presume this would also work for signed values by wiring type to I32.

DonRoth wrote:

> I am importing an NDF data file using labview read file VIs. The
> header of this file is composed of various strings and I32 type
> values. The strings read in fine. When attempting to read the I32
> type, I am getting incorrect numbers. (I specify I32 type for the
> input on the Read file fx.) For example, header size should be '2048'
> but I am getting '524288'. Data Points should be '1024'
but I am
> getting '262144.' I was told I need to convert to little endian
> format. Byte swapping is trivial for I16/U16 but it is not clear how
> to attack this with I32 type. Given that we know what the values
> should be, I was hoping to be able to reverse engineer this somehow.
> I attempted various byte operations from the labview data manipulation
> palette with no success. Any advise here is appreciated.
>
> Thanks,
>
> Don
0 Kudos
Message 5 of 15
(8,569 Views)
Someone correct me if I am wrong, but I think that Big/Little Endian has to do with byte order, not bit order. If so, there is no need to convert numbers to bits and reverse their order. The split number and join number functions (Advanced - Data Manipulation - Split Number, Join Number), does an easy job of splitting words and bytes and joining bytes and words. To convert from one endian to another, split the number down to 8 bit sections, then join back together in reverse order. See the attached example. The numeric control and indicator are set for hex display in order to easily see the byte splitting/joining. If you change from hex to decimal display and enter 2048, you will see an output of 524288, and vice versa. This conversion vi could be ma
de more simple by eliminating the intermediate numerical indicators. I put them here for illustration. This should be an easy fix for your problem. It will convert from one endian case to the other, doesn't matter which case is the input.
- tbob

Inventor of the WORM Global
0 Kudos
Message 6 of 15
(8,568 Views)
Hi tbob
You are right if working wwith integers(I16,U16,I32,U32) but little endian/big endian also applies to float (SGL,DBL,....) so, in this case, the Typecast to U8 array, reverse array and typecast back to float type is the best choice in Labview, I think.

Cheers
Alipio
---------------------------------------------------------
"Qod natura non dat, Salmantica non praestat"
---------------------------------------------------------
0 Kudos
Message 7 of 15
(8,568 Views)
Well thanks to George Gatling, he suggested something I had not tried yet regarding reversing the bytes and it works. Here is his solution:

You can use the "Split Number" and "Join Number" function on the data manipulation palette to accomplish this. Split the I32 once to get I16 and then split each I16 once to get four I8. Then join the numbers back together in the reverse order (ie with the highest source byte as the lowest target byte). And this will do the trick!
0 Kudos
Message 8 of 15
(8,568 Views)
Well thanks to a few folks on info-labview mailing list, we have an even easier method:

Byte swap, then word swap. This will reverse all the bytes in the I32. Also, if you read the help on these fxs, they appear to be polymorphic and would therefore be quite versatile.

Don
0 Kudos
Message 9 of 15
(8,177 Views)
Based on our discussions, I just put a byte swapper together for dbl type if anyone wants it. A general byte swapper can be envisioned with a case for each class (I32, I64, dbl, sgl, array? etc.) by programatically obtaining the cases from the classID or typedescriptor. What I did for the dbl type was first typecast it into I32 array and then do the byte swap word swap procedure, followed by rebuilding array, reversing array, and retypecasting back to dbl..Don
Message 10 of 15
(8,177 Views)