How to split a string into multiple floats?

Hello, does anyone know how to split a string into several separate floats? I’m receiving an array of bytes from my Bluetooth controller, which sends some data from gyroscope, compass and accelerometer sensors. The lines which it sends look like that:

0.45 -145 63.5

0.46 148 63.3

0.43 125 68.3

0.22 - 165 68.3

Where these are Accel. value / Gyro. value / Comp. value separated with just spaces like " "

The way I convert the array of bytes into a string will, perhaps, be something like this:

byte [] msg = device.read ();
				string content = System.Text.ASCIIEncoding.ASCII.GetString (msg);

How can I split this string into 3 corresponding float numbers?

Perhaps I need something like this:

float accelerometer = float.Parse (certain string part);
float Gyroscope = float.Parse (certain string part);
float Compass = float.Parse (certain string part);

but I don’t really know how to do that properly.

Can anyone help?

Make sure you are using:

using System;

And use the following code to convert a string to a float array if the values are seperated by a space character:

string data = "0.45 -145 63.5";
float[] floatData = Array.ConvertAll(data.Split(','), float.Parse);

Still not working.

This method:

string data = "0.45 -145 63.5"; float[] floatData = Array.ConvertAll(data.Split(','), float.Parse); float float1 = floatData[0]; float float2 = floatData[1]; float float3 = floatData[2];

gives error: "The type arguments for method

System.Array.ConvertAll(TInput, System.Converter)’ cannot be inferred from the usage. Try specifying the type arguments explicitly"

Whatever I do, it won’t work.

The code that I’m using is this:

IEnumerator  ManageConnection (BluetoothDevice device)

		while (device.IsConnected && device.IsReading) {
			if (device.IsDataAvailable) {
byte [] msg = device.read ();
  string content = System.Text.ASCIIEncoding.ASCII.GetString (msg);

 float [] floatData = Array.ConvertAll(content.Split(','), float.Parse);

float Acceleration = floatData[0];
float Gyroscope = floatData[1];
float Compass = floatData[2];

     }

			yield return null;
		}

The data that I receive via Bluetooth controller in this code is like :

0.12 -45.33 88.33

where the values are divided with just (" “) and the string end is divided with (”
") (I’m using an Arduino that reads sensor data and sends it via bluetooth).

Also, this thing is running on an Android device, which has some different settings for decimal separator, so, previously, when I was reading only one value in each received string, I was using this :

float floatData = float.Parse (content, System.Globalization.CultureInfo.InvariantCulture);

to fix the problem. But now, when I need to receive multiple sensors’ data in one line, I have this problem as well.

Does anybody know what the solution might be like?

hey did you solve it …i’m stuck on the same thing …and dont know how to get through.

Thanks man … I’m able to send data as 13.1,-45.2,67.5, to terminal app but couldn’t able to use in unity … After reading data as

byte msg= device.read();

and converting it into string as you have mentioned in previous codes …it not working for me … Can you tell me at what baudrate you have set for Arduino sketch as well as HC05 Bluetooth module .

Thanks man … I’m able to send data as 13.1,-45.2,67.5, to terminal app but couldn’t able to use in unity … After reading data as

byte msg= device.read();

and converting it into string as you have mentioned in previous codes …it not working for me … Can you tell me at what baudrate you have set for Arduino sketch as well as HC05 Bluetooth module .