Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Rasca · May 04, 2014 at 02:46 PM · rigidbodymultiplayerfloatconvertbyte

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

I use google play game services for unity. Send packet look like this:

byte[] mPosPacket = new byte[4];

public void BroadCastPosition() {

 mPosPacket[0] = (byte) 'P';
 mPosPacket [1] = (byte)position.x;
 mPosPacket [2] = (byte)position.y;
 mPosPacket [3] = (byte)position.z;
     PlayGamesPlatform.Instance.RealTime.SendMessageToAll(false, mPosPacket);
 }

Recive look like this:

public void OnRealTimeMessageReceived(bool isReliable, string senderId, byte[] data) {

     if (data [0] == (byte)'P') {
         posX = (float)data [1];
         posY = (float)data [2];
         posZ = (float)data [3];
     }

With this everything work but for example when send float 2.4243, recive float 2. I need more accurate. When I use System.bitconverter float is super accurate but i can't give something like this "mPosPacket[0] = (byte) 'P';" Anyone know way to recognize byte or use bitconverter to start with 1 index?

I need something like this:

mPosPacket[0] = (byte) 'P';

mPosPacket[1], mPosPacket[2], mPosPacket[3] is one float

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Jeff-Kesselman · May 04, 2014 at 03:02 PM

Use DataStream to convert primitive types to their byte repesentations and back

http://msdn.microsoft.com/en-us/library/aa271346(v=vs.60).aspx

Edit: Actually in c# you use BinaryWriter, my mistake. Its the same basic thing though

 float f = 3.1415;
 MemoryStream stream = new MemoryStream();
 BinaryWriter bw = new BinaryWriter(stream);
 bw.Write(f);
 bw.Flush();
 byte[] floatBytes = stream.ToArray();

Do the reverse with BinaryReader to convert back.

Note that, rather then doing this for each float, you can create one stream that has all your data in it with successive writes and get it back with successive reads.

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 Rasca · May 04, 2014 at 04:54 PM 0
Share

How use DataStream in Unity? Can you write a example?

avatar image Rasca · May 07, 2014 at 03:12 PM 0
Share

How can you use $$anonymous$$emoryStream and BinaryWriter in Unity? I don't have this class.

avatar image roojerry · May 07, 2014 at 03:45 PM 0
Share

You just need to make sure you are importing System.IO

in C# you do this as the top of the file with:

 using System.IO;
avatar image Rasca · May 07, 2014 at 05:40 PM 0
Share

How can I read specific float, when I (send, then) recive only byte[]?

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

23 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

Related Questions

Converting a float to a byte[]? Not working 2 Answers

Conversion of float[] to byte[] is too slow, how to optimize this? 0 Answers

Multiplayer Rigidbody Vibration 2 Answers

convert object to float 2 Answers

How to convert 00:40:00.0 to float 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