• 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, w$$anonymous$$ch sends some data from gyroscope, compass and accelerometer sensors. The lines w$$anonymous$$ch 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 somet$$anonymous$$ng like t$$anonymous$$s:

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

How can I split t$$anonymous$$s string into 3 corresponding float numbers?

Perhaps I need somet$$anonymous$$ng like t$$anonymous$$s:

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
avatar image M-Hanssen Mike Last · Apr 13, 2016 at 07:04 AM 0
Share
avatar image Mike Last M-Hanssen · Apr 13, 2016 at 07:33 AM 0
Share
Show more comments
avatar image
0

Answer by Mike Last · Apr 19, 2016 at 11:46 AM

Still not working.

T$$anonymous$$s 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 t$$anonymous$$s:

 IEnumerator  ManageConnection (BluetoothDevice device)
 
         w$$anonymous$$le (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 t$$anonymous$$s 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, t$$anonymous$$s t$$anonymous$$ng is running on an Android device, w$$anonymous$$ch has some different settings for decimal separator, so, previously, when I was reading only one value in each received string, I was using t$$anonymous$$s :

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 t$$anonymous$$s 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
avatar image Hellium TRINITY9297 · Jan 25, 2019 at 11:49 AM 2
Share
avatar image TRINITY9297 Hellium · Jan 25, 2019 at 12:21 PM 0
Share
avatar image TRINITY9297 · Jan 25, 2019 at 12:18 PM 0
Share
avatar image Hellium TRINITY9297 · Jan 25, 2019 at 12:53 PM 0
Share
avatar image TRINITY9297 Hellium · Jan 25, 2019 at 01:28 PM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

the floats wont be applied, Random.Range 1 Answer

Smooth Damp Angle vs Smooth Damp Float? 1 Answer

Float calculation always outputting 0 when multiplied with another float.... 1 Answer

Floats not being added to other floats? 1 Answer

Min and Max value? 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