Search code examples
c#labview

How to convert this logic from LabVIEW to C# - hex string to byte array and type cast the byte array to single


LabVIEW code:

hex string is converted in to byte array then it is typecast to single

C# code I tried:

var freqArray = new byte[] { 67, 179, 84, 45 };  

// 358.658

var r1 = BitConverter.ToDouble(freqArray, 0);

Solution

  • First you are dealing with a 4 byte values and double require 8 bytes. What you want is using single as it's a 4 bytes.

    var r1 = BitConverter.ToSingle(freqArray, 0);
    

    Secondly your array is backward and it should be :

    var freqArray = new byte[] { 45, 84 , 179, 67 };