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?
Answer by M-Hanssen · Apr 12, 2016 at 08:55 PM
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);
Thank you for the answer. I'll try to do so. Do you know how could I get 3 floats then, after I got this float [] array? I need to get float 1; float 2; float 3 out of that array in order to manipulate this data afterwards. Where float 1 would be = 0.45 ; float 2 = -145; float 3 = 63.6, and so on with every new updated line. Thanks!
This is basic stuff! Please read some more on arrays or C# in general. dotnetperls.com is a great place to start.
In order to get the float values you must retreive them from the array by a certain index:
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];
Oh, I see, thats really simple! Thanks a lot. I'm new to C# and Unity, and not really well familiar with this stuff. Thanks again, I'll check it out asap.
Answer by Mike Last · Apr 19, 2016 at 11:46 AM
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 ("\t") and the string end is divided with ("\n") (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.
string data = "0.45 -145 63.5";
string[] stringValues = data.Split(' ');
float[] floatValues = new float[stringValues.Length];
for( int i = 0 ; i < floatValues.Length ; ++i )
floatValues[i] = float.Parse( stringValues[i].Trim() ) ;
Can you please check out this and tell me what I can do
https://stackoverflow.com/q/54347825/3615685
The answer you have mentioned above , I'll try and let you know . Thanks.
Thanks man ...... I'm able to send data as 13.1,-45.2,67.5, to ter$$anonymous$$al 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 .
Have you tried to print the output string?
Be careful, in my code above, the separator is a space character. You will need to change it according to the received string (in your case, you will have to replace it with a comma ,
)
ya I did tried to print the output string after conversion ...but shows nothing .....and its is in if(packets != null ) so certainly id does getting some data .... and i'm sure that my Bluetooth module is sending the data coz I have checked with the ter$$anonymous$$al app available on Playstore and yep as you pointed out I will make sure to use the code as you have mentioned.