Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace PWAPI.Packets
- {
- static class Extensions
- {
- public static byte[] HexString2Bytes(string hex)
- {
- if (hex == null) return new byte[0];
- if (hex.Length < 2) return new byte[0];
- try
- {
- hex = hex.Replace("-", "");
- hex = hex.Replace(" ", "");
- return Enumerable.Range(0, hex.Length).Where(x => x % 2 == 0).Select(x => Convert.ToByte(hex.Substring(x, 2), 16)).ToArray();
- }
- catch (Exception ex)
- {
- Console.WriteLine("[FATAL] CONVERTING ERROR: {0}", ex.Message);
- return new byte[0];
- }
- }
- }
- static class Convertation
- {
- public static UInt16 ReverseBytes(UInt16 value)
- {
- return (UInt16)((value & 0xFFU) << 8 | (value & 0xFF00U) >> 8);
- }
- public static UInt32 ReverseBytes(UInt32 value)
- {
- return (value & 0x000000FFU) << 24 | (value & 0x0000FF00U) << 8 |
- (value & 0x00FF0000U) >> 8 | (value & 0xFF000000U) >> 24;
- }
- public static UInt64 ReverseBytes(UInt64 value)
- {
- return (value & 0x00000000000000FFUL) << 56 | (value & 0x000000000000FF00UL) << 40 |
- (value & 0x0000000000FF0000UL) << 24 | (value & 0x00000000FF000000UL) << 8 |
- (value & 0x000000FF00000000UL) >> 8 | (value & 0x0000FF0000000000UL) >> 24 |
- (value & 0x00FF000000000000UL) >> 40 | (value & 0xFF00000000000000UL) >> 56;
- }
- public static float ReverseBytes(float value)
- {
- return BitConverter.ToSingle(ReverseBytes(BitConverter.GetBytes(value)), 0);
- }
- public static byte[] ReverseBytes(byte[] value)
- {
- return value.Reverse().ToArray();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement