Skip to content

Instantly share code, notes, and snippets.

@SuperIzzo
Created April 4, 2015 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SuperIzzo/0e75ec4497c36adf928a to your computer and use it in GitHub Desktop.
Save SuperIzzo/0e75ec4497c36adf928a to your computer and use it in GitHub Desktop.
.Net BitArray serialization and deserialization example in Unity. A lot of room for improvement, but that's the gist.
using UnityEngine;
using System.Collections;
public class BitArrayTest : MonoBehaviour
{
// Use this for initialization
void Start ()
{
BitArray array = new BitArray( new bool[]{true, false, true, true, false, false} );
Debug.Log( StrBits(array) );
string str = Serialize( array );
Debug.Log( str );
array = Deserialize( str );
Debug.Log( StrBits(array) );
}
string StrBits( BitArray array )
{
string result = "";
foreach( bool bit in array )
{
if( bit )
result += "1";
else
result += "0";
}
return result;
}
string Serialize( BitArray array )
{
byte[] byteArray = new byte[array.Length/8+1];
array.CopyTo( byteArray, 0 );
return System.Text.Encoding.UTF8.GetString( byteArray );
}
BitArray Deserialize( string str )
{
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes( str );
return new BitArray( byteArray );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment