Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public unsafe static class FastConverter
- {
- static readonly byte[] HiTable =
- {
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 0, 16,
- 32, 48, 64, 80, 96, 112, 128, 144, 255, 255,
- 255, 255, 255, 255, 255, 160, 176, 192, 208, 224,
- 240, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 160, 176, 192,
- 208, 224, 240
- };
- static readonly byte[] LoTable =
- {
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 0, 1,
- 2, 3, 4, 5, 6, 7, 8, 9, 255, 255,
- 255, 255, 255, 255, 255, 10, 11, 12, 13, 14,
- 15, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
- 255, 255, 255, 255, 255, 255, 255, 10, 11, 12,
- 13, 14, 15
- };
- public static byte[] FromHexString(string source)
- {
- // No checks, quality (not null and even chars) of source garanteed
- var result = new byte[source.Length >> 1];
- fixed (char* sourceRef = source)
- fixed (byte* resultRef = result)
- {
- fixed (byte* hiRef = HiTable)
- fixed (byte* lowRef = LoTable)
- {
- char* s = &sourceRef[0];
- byte* r = resultRef;
- while (*s != 0)
- {
- *r = hiRef[*s++];
- *r++ += lowRef[*s++];
- }
- return result;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement