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 /
avatar image
0
Question by 12boulla · Dec 26, 2015 at 07:23 PM · multiplayerfloatgoogle play gamesbytearraybyte

Converting a float to a byte[]? Not working

Okay, so all I simply need is to convert float (e.g 3.422) and turn it into a byte[].

I need this because I am sending the byte as data for real time multiplayer. Everywhere I look online just have really complicated and different answers. I just need a simple way to convert my float into a byte array.

At present, I am using this:

 float f = transform.position.y;
     MemoryStream stream = new MemoryStream();
     BinaryWriter bw = new BinaryWriter(stream);
     bw.Write(f);
     bw.Flush();

     byte[] floatBytes = stream.ToArray();
     bool reliable = true;
     PlayGamesPlatform.Instance.RealTime.SendMessageToAll (reliable, floatBytes);

But i dont know how to read this later on. So far I have:

 MemoryStream stream = new MemoryStream ();
 BinaryReader reader = new BinaryReader (stream);

But I don't know what do do from there.

Any help is greatly appreciated, thanks.

Comment
Add comment · Show 1
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 Measurity · Dec 26, 2015 at 11:23 PM 0
Share

If you only want to convert bytes to floats and back then use the BitConverter class. It has a BitConverter.GetBytes(float) method that returns the bytes.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Munchy2007 · Dec 26, 2015 at 08:28 PM

Use float value = reader.ReadSingle();

Comment
Add comment · Show 1 · 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 12boulla · Dec 26, 2015 at 08:34 PM 0
Share

But how do I specify which byte array to read? Say I have byte[] myByteArray, how do i read the float from that?

Do I do $$anonymous$$emoryStream stream = new $$anonymous$$emoryStream(myByteArray)?

Thanks

avatar image
0

Answer by Measurity · Dec 27, 2015 at 01:00 AM

Use C#'s BitConverter class:

 BitConverter.GetBytes(3.422)
Comment
Add comment · 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

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

47 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

Related Questions

How change float to byte[] and then byte[] to float without accuracy lost? 1 Answer

How to Synchronize Play Services Real-Time Multiplayer 0 Answers

Google Nearby Unity API crash 0 Answers

Converting Vector3 to Byte[]? 4 Answers

How To Reconnect to a room in google real time muliplayer 2 Answers


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