Created
April 4, 2015 21:24
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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