1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MSHO.Collection.Service
- {
- public static class Helper {
-
- public static byte[] Int32ToBytes(this int n) {
- var bytes = new byte[4];
- bytes[0] = (byte)(n & 0xFF);
- bytes[1] = (byte)(n >> 8 & 0xFF);
- bytes[2] = (byte)(n >> 16 & 0xFF);
- bytes[3] = (byte)(n >> 24 & 0xFF);
- return bytes;
- }
- public static int BytesToInt32(this byte[] bytes, int index = 0) {
- int n = 0;
- if (bytes != null && bytes.Length >= 4) {
- n = bytes[index + 3];
- n = n << 8 | bytes[index + 2];
- n = n << 8 | bytes[index + 1];
- n = n << 8 | bytes[index + 0];
- }
- return n;
- }
- public static byte[] LongToBytes(this long n)
- {
- var bytes = new byte[sizeof(long)];
- for (var i = 0; i < sizeof(long); i++) {
- bytes[i] = (byte)((n >> (i*8)) & 0xFF);
- }
- return bytes;
- }
- public static long BytesToLong(this byte[] bytes, int index = 0)
- {
- long n = 0;
- if (bytes != null && bytes.Length >= sizeof(long))
- {
- for (var i = sizeof(long) - 1; i >=0; i--) {
- n = n << 8 | bytes[index + i];
- }
- }
- return n;
- }
- public static T GetEnum<T>(string val, T d) where T : struct
- {
- T result = default(T);
- if (!Enum.TryParse<T>(val, out result))
- {
- result = d;
- }
- return result;
- }
- }
- }
|