Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Mike Last · Apr 12, 2016 at 07:10 PM · floatsstring.split

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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);
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mike Last · Apr 13, 2016 at 06:42 AM 0
Share

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!

avatar image M-Hanssen Mike Last · Apr 13, 2016 at 07:04 AM 0
Share

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];

avatar image Mike Last M-Hanssen · Apr 13, 2016 at 07:33 AM 0
Share

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.

Show more comments
avatar image
0

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?

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TRINITY9297 · Jan 25, 2019 at 06:21 AM 0
Share

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

avatar image Hellium TRINITY9297 · Jan 25, 2019 at 11:49 AM 2
Share
  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() ) ;
avatar image TRINITY9297 Hellium · Jan 25, 2019 at 12:21 PM 0
Share

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.

avatar image TRINITY9297 · Jan 25, 2019 at 12:18 PM 0
Share

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 .

avatar image Hellium TRINITY9297 · Jan 25, 2019 at 12:53 PM 0
Share

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 ,)

avatar image TRINITY9297 Hellium · Jan 25, 2019 at 01:28 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

57 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to move player using Coroutine? 1 Answer

float to bool retardedness 2 Answers

problem comparing Strings using compareTo() as it never evaluates to 0 even when the two strings are equal 0 Answers

Make an int/float all numbers between two numbers? (In a range) 1 Answer

Split string: keep split chars separators C# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges