Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

[SOLVED] ByteArray to convert ... Impossible ?

Discussion in 'Scripting' started by Pulsar19, Dec 16, 2018.

  1. Pulsar19

    Pulsar19

    Joined:
    Feb 21, 2018
    Posts:
    15
    Hello,

    I have a big problem :
    During a project, I get a byte array contains the following values: '' 0 48 0 3 63 83 91 53 ... ''
    It would appear that "0 48 0 3" means 3145731 (int), while "63 83 91 53" means 0.82561 (float).

    The only information I have for this byte array is: *All of the values are in big-endian order and it's a format in 4-byte words.*

    However, I do not arrive at these results, I tried everything and all converter ...
    Someone would have any idea please ?
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class TestScript : MonoBehaviour
    7. {
    8.     public static Byte[] test = new Byte[] { 0, 48, 0, 3, 63, 83, 91, 53 };
    9.  
    10.     private void Awake()
    11.     {
    12.         byte[] tempByteArray = new byte[4];
    13.  
    14.         for(int i=0; i < test.Length; i+=4)
    15.         {
    16.             tempByteArray[0] = test[i];
    17.             tempByteArray[1] = test[i+1];
    18.             tempByteArray[2] = test[i+2];
    19.             tempByteArray[3] = test[i+3];
    20.  
    21.             if(BitConverter.IsLittleEndian)
    22.                 Array.Reverse(tempByteArray);
    23.             int tempInt = BitConverter.ToInt32(tempByteArray, 0);
    24.             Debug.Log(tempInt);
    25.         }
    26.     }
    27. }
    28.  
    And of course you can optimize this whole thing a whole lot. It was just a crude quickie for ints.

    But, and it's important: you don't know if the value is float or int. Is there anything which identifies such cases?
     
    Last edited: Dec 16, 2018
    Pulsar19 likes this.
  3. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    I get those result

    Code (CSharp):
    1.             var test = new byte[] { 0, 48, 0, 3, 63, 83, 91, 53 };
    2.             if (BitConverter.IsLittleEndian)
    3.                 Array.Reverse(test);
    4.  
    5.             var f = BitConverter.ToSingle(test, 0);
    6.             var i = BitConverter.ToInt32(test, 4);
     
    Pulsar19 likes this.
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Not sure how you're supposed to tell the difference between the data type just by the received value, is there any additional data you receive? If not, that's only possible when your code knows the pattern that was used to write the values.

    However, if you only expect either of both types, I wouldn't convert every single entry, but instead:
    • determine the endianess of the system you're running on, as shown by the other posts and reverse it if necessary.
    • make a quick copy using System.Buffer.BlockCopy from the byte array to either int or float array
    • if you didn't reverse it, you're done - if you reversed it, you can either read the array backwards or reverse it and read it normally
     
    Pulsar19 likes this.
  5. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    I'm guessing it's position based by a convention?
     
    Pulsar19 likes this.
  6. Pulsar19

    Pulsar19

    Joined:
    Feb 21, 2018
    Posts:
    15
    Hey, thank you for your answers !

    In fact, my byte array has this same repeated pattern. I will have (always) an int then float then int then float ... several times: {int0, float0, int1, float1, int2, float2, ...} (I dont know how much time but I have my bytearray size).

    The idea is :
    I wanted to check in a Vector2 list if for my first element Vector2[0].X = int0 so I put Vector2[0].Y = float0 (I do this for all).
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Note that the equality operator in float == int will implictly convert the integer to float, which is okay, but you'll end up comparing exact floats. So if the x value of the vector does not exactly represent the converted integer, it'll return false. In such cases, you should use Mathf.Approximately.
    If you want to floor, ceil or round the float to an integer for the comparison, you'll find the corresponding methods in the Mathf class as well.
     
    Pulsar19 likes this.
  8. Pulsar19

    Pulsar19

    Joined:
    Feb 21, 2018
    Posts:
    15
    Ah ok, thank you !

    [Resolved]