diff --git a/Lidgren.Network/NetUtility.cs b/Lidgren.Network/NetUtility.cs index 2bbcb65..259a1e8 100644 --- a/Lidgren.Network/NetUtility.cs +++ b/Lidgren.Network/NetUtility.cs @@ -143,14 +143,18 @@ namespace Lidgren.Network public static string ToHexString(byte[] data) { - StringBuilder sb = new StringBuilder(data.Length * 2); - foreach (byte b in data) + char[] c = new char[data.Length * 2]; + byte b; + for (int i = 0; i < data.Length; ++i) { - sb.AppendFormat("{0:X2}", b); + b = ((byte)(data[i] >> 4)); + c[i * 2] = (char)(b > 9 ? b + 0x37 : b + 0x30); + b = ((byte)(data[i] & 0xF)); + c[i * 2 + 1] = (char)(b > 9 ? b + 0x37 : b + 0x30); } - return sb.ToString(); + return new string(c); } - + /// /// Gets my local IP address (not necessarily external) and subnet mask /// diff --git a/UnitTests/MiscTests.cs b/UnitTests/MiscTests.cs index 6f2c71b..cfdd6f2 100644 --- a/UnitTests/MiscTests.cs +++ b/UnitTests/MiscTests.cs @@ -19,6 +19,8 @@ namespace UnitTests throw new NetException("setting enabled message types failed"); Console.WriteLine("Misc tests OK"); + + Console.WriteLine("Hex test: " + NetUtility.ToHexString(new byte[]{0xDE,0xAD,0xBE,0xEF})); } } }