aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Stubs Collection/stubs/Scripts/ConvertFloat2Bytes.cs
blob: c199b9c2992ff598877f6b706e35d2cda9a8ab22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Text;
using System.Linq;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using Tango.PMR.Stubs;
using Tango.Stubs;



public void OnExecute(StubManager stubManager)
{
	//see online Floating Point to Hex Converter (with Swap endianness) :https://gregstoll.com/~gregstoll/floattohex/	
	// ----------------- option 1  -------------------------------
	stubManager.WriteLine("--- option 1 ---");
	float value = 5.2F;
	byte [] byteArray1 = new byte[4];// a single float is 4 bytes/32 bits
	byteArray1 = BitConverter.GetBytes(value);
	
	//print
	for(int i =0;i<4;i++)
	{		
		stubManager.WriteHex(byteArray1[i],2);
		stubManager.Write(" ");
	}
		stubManager.WriteLine("");
		stubManager.WriteLine("");
		
	// ----------------- option 2 for buffer  -------------------------------
	stubManager.WriteLine("--- option 2 ---");
	var floatArray1 = new float[] {5.2f, 1.2f, 3.7f};
	
	
	// create a byte array and copy the floats into it...
	var byteArray = new byte[floatArray1.Length * 4];// a single float is 4 bytes/32 bits
	Buffer.BlockCopy(floatArray1, 0, byteArray, 0, byteArray.Length);
	
	//print
	for(int j =0;j<floatArray1.Length;j++)
	{		
		for(int i =0;i<4;i++)
		{		
			stubManager.WriteHex(byteArray[j+i],2);
		    stubManager.Write(" ");
		}
		stubManager.WriteLine("");
	}
	
	
}