Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static byte[] SerializeMap(bool[,] map) {
- byte[] arr = new byte[map.Length + 8];
- byte[] widthBytes = BitConverter.GetBytes(map.GetLength(0));
- byte[] heigthBytes = BitConverter.GetBytes(map.GetLength(1));
- arr[0] = widthBytes[0];
- arr[1] = widthBytes[1];
- arr[2] = widthBytes[2];
- arr[3] = widthBytes[3];
- arr[4] = heigthBytes[0];
- arr[5] = heigthBytes[1];
- arr[6] = heigthBytes[2];
- arr[7] = heigthBytes[3];
- int position = 8;
- for (int x = 0; x < map.GetLength(0); x++) {
- for (int y = 0; y < map.GetLength(1); y++) {
- arr[position] = BitConverter.GetBytes(map[x, y])[0];
- position++;
- }
- }
- return arr;
- }
- public static bool[,] DeserializeMap(byte[] arr) {
- int width = BitConverter.ToInt32(arr, 0);
- int heigth = BitConverter.ToInt32(arr, 4);
- bool[,] map = new bool[width, heigth];
- int position = 8;
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < heigth; y++) {
- map[x, y] = BitConverter.ToBoolean(arr, position);
- position++;
- }
- }
- return map;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement