From 31b281017844517d142155137c6f55e824805db7 Mon Sep 17 00:00:00 2001 From: C9Glax <13404778+C9Glax@users.noreply.github.com> Date: Wed, 8 Feb 2023 18:00:19 +0100 Subject: [PATCH] Fixed bugs with byte[] sizes --- OSMDatastructure/ByteConverter.cs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/OSMDatastructure/ByteConverter.cs b/OSMDatastructure/ByteConverter.cs index adb7216..de0833e 100644 --- a/OSMDatastructure/ByteConverter.cs +++ b/OSMDatastructure/ByteConverter.cs @@ -91,11 +91,13 @@ public static class ByteConverter public static byte[] GetBytes(OsmNode node) { - int totalBytes = 4 + 8; //size + Coordinates + int totalBytes = sizeof(int) + 8; //size + Coordinates HashSet edges = new(); foreach (OsmEdge edge in node.edges) { - edges.Add(GetBytes(edge)); + byte[] edgeBytes = GetBytes(edge); + edges.Add(edgeBytes); + totalBytes += edgeBytes.Length; } byte[] retBytes = new byte[totalBytes]; @@ -158,6 +160,7 @@ public static class ByteConverter tagsSize += tagBytes.Length; } + totalBytes += tagsSize; byte[] retBytes = new byte[totalBytes]; Array.Copy(BitConverter.GetBytes(tagsSize), retBytes, 4); Array.Copy(GetBytes(edge.neighborCoordinates), 0, retBytes, sizeof(int), 8); @@ -206,32 +209,37 @@ public static class ByteConverter public static byte[] GetBytes(KeyValuePair tag) { - byte[] objectBytes = Array.Empty(); - if (tag.Value.GetType() == Type.GetType("string")) + byte[] objectBytes; + Type objectType = tag.Value.GetType(); + if (objectType == Type.GetType("System.String")) { objectBytes = Encoding.ASCII.GetBytes((string)tag.Value); } - else if (tag.Value.GetType() == Type.GetType("bool")) + else if (objectType == Type.GetType("System.Boolean")) { objectBytes = BitConverter.GetBytes((bool)tag.Value); } - else if (tag.Value.GetType() == Type.GetType("int")) + else if (objectType == Type.GetType("System.Int32")) { objectBytes = BitConverter.GetBytes((int)tag.Value); } - else if (tag.Value.GetType() == Type.GetType("ulong")) + else if (objectType == Type.GetType("System.UInt64")) { objectBytes = BitConverter.GetBytes((ulong)tag.Value); } - else if (tag.Value.GetType() == Type.GetType("byte")) + else if (objectType == Type.GetType("System.Byte") || objectType.Name == "wayType") { objectBytes = new[] { (byte)tag.Value }; } + else + { + throw new Exception(string.Format("Tag-Bytes object-Type: {0}", tag.Value.GetType())); + } byte[] retBytes = new byte[sizeof(int) + 1 + objectBytes.Length]; Array.Copy(BitConverter.GetBytes(objectBytes.Length), 0, retBytes, 0, sizeof(int)); retBytes[sizeof(int)] = (byte)tag.Key; - Array.Copy(objectBytes, 0, retBytes, sizeof(int) + 1, retBytes.Length); + Array.Copy(objectBytes, 0, retBytes, sizeof(int) + 1, objectBytes.Length); return retBytes; }