ModernUO/Projects/Server/Utilities/HexStringConverter.cs
Kamron Batman 369a27b800
Replacing Networking (#271)
- [X] Removing Kestrel & Libuv
- [X] Cleaning up NetState
- [X] Removing System.IO.Pipelines
- [X] Cleaning up packet reading
- [X] Adds a maximum of 5000 sockets (configurable) to prevent OOM
- [X] Replaces the AsyncState with a thread-safe wrapped boolean called NetworkState
- [X] Removes Parallel.ForEach (no perf gain)
- [X] Removes custom houses compression on another thread
- [X] Test high load scenarios

Bumps release version
2020-10-20 20:55:19 -07:00

80 lines
2.9 KiB
C#

/*************************************************************************
* ModernUO *
* Copyright 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: HexStringConverter.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
namespace Server
{
public class HexStringConverter
{
public static readonly uint[] m_Lookup32Chars = CreateLookup32Chars();
private static uint[] CreateLookup32Chars()
{
var result = new uint[256];
for (var i = 0; i < 256; i++)
{
var s = i.ToString("X2");
if (BitConverter.IsLittleEndian)
{
result[i] = s[0] + ((uint)s[1] << 16);
}
else
{
result[i] = s[1] + ((uint)s[0] << 16);
}
}
return result;
}
public static unsafe string GetString(ReadOnlySpan<byte> bytes)
{
var result = new string((char)0, bytes.Length * 2);
fixed (char* resultP = result)
{
var resultP2 = (uint*)resultP;
for (var i = 0; i < bytes.Length; i++)
{
resultP2[i] = m_Lookup32Chars[bytes[i]];
}
}
return result;
}
public static unsafe void GetBytes(string str, Span<byte> bytes)
{
fixed (char* strP = str)
{
var i = 0;
var j = 0;
while (i < str.Length)
{
int chr1 = strP[i++];
int chr2 = strP[i++];
if (BitConverter.IsLittleEndian)
{
bytes[j++] = (byte)(((chr1 - (chr1 >= 65 ? 55 : 48)) << 4) | (chr2 - (chr2 >= 65 ? 55 : 48)));
}
else
{
bytes[j++] = (byte)((chr1 - (chr1 >= 65 ? 55 : 48)) | ((chr2 - (chr2 >= 65 ? 55 : 48)) << 4));
}
}
}
}
}
}