Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace PWAPI.Packets
  5. {
  6.     static class Extensions
  7.     {
  8.         public static byte[] HexString2Bytes(string hex)
  9.         {
  10.             if (hex == null) return new byte[0];
  11.             if (hex.Length < 2) return new byte[0];
  12.             try
  13.             {
  14.                 hex = hex.Replace("-", "");
  15.                 hex = hex.Replace(" ", "");
  16.                 return Enumerable.Range(0, hex.Length).Where(x => x % 2 == 0).Select(x => Convert.ToByte(hex.Substring(x, 2), 16)).ToArray();
  17.             }
  18.             catch (Exception ex)
  19.             {
  20.                 Console.WriteLine("[FATAL] CONVERTING ERROR: {0}", ex.Message);
  21.                 return new byte[0];
  22.             }
  23.         }
  24.     }
  25.  
  26.     static class Convertation
  27.     {
  28.         public static UInt16 ReverseBytes(UInt16 value)
  29.         {
  30.             return (UInt16)((value & 0xFFU) << 8 | (value & 0xFF00U) >> 8);
  31.         }
  32.  
  33.         public static UInt32 ReverseBytes(UInt32 value)
  34.         {
  35.             return (value & 0x000000FFU) << 24 | (value & 0x0000FF00U) << 8 |
  36.                    (value & 0x00FF0000U) >> 8 | (value & 0xFF000000U) >> 24;
  37.         }
  38.  
  39.         public static UInt64 ReverseBytes(UInt64 value)
  40.         {
  41.             return (value & 0x00000000000000FFUL) << 56 | (value & 0x000000000000FF00UL) << 40 |
  42.                    (value & 0x0000000000FF0000UL) << 24 | (value & 0x00000000FF000000UL) << 8 |
  43.                    (value & 0x000000FF00000000UL) >> 8 | (value & 0x0000FF0000000000UL) >> 24 |
  44.                    (value & 0x00FF000000000000UL) >> 40 | (value & 0xFF00000000000000UL) >> 56;
  45.         }
  46.  
  47.         public static float ReverseBytes(float value)
  48.         {
  49.             return BitConverter.ToSingle(ReverseBytes(BitConverter.GetBytes(value)), 0);
  50.         }
  51.  
  52.         public static byte[] ReverseBytes(byte[] value)
  53.         {
  54.             return value.Reverse().ToArray();
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement