public DataPage(byte[] page) { _byteIterator = 0; Records = new List <Record> { Capacity = Static.GetRecordsPerPage() }; UsedSpace = BC.ToInt32(page, _byteIterator); _byteIterator += sizeof(int); for (var i = 0; i < UsedSpace; i++) { var key = BC.ToUInt32(page, _byteIterator); _byteIterator += sizeof(uint); var radius = BC.ToDouble(page, _byteIterator); _byteIterator += sizeof(double); var angle = BC.ToInt16(page, _byteIterator); _byteIterator += sizeof(short); Records.Add(new Record(key, radius, angle)); } }
private void _ReadEmblemFullInfo() { //loop here in case it runs this too early and reads 0 max emblems because it's not initialized in the game yet int tries = 0, maxEmblems = 0, maxExtra = 0; do { //read the number of emblems loaded into the game byte[] maxEmblemsBuffer = new byte[4]; byte[] maxExtraBuffer = new byte[4]; ReadProcessMemory(gameProc.Handle, MAX_EMBLEMS_ADDRESS, maxEmblemsBuffer, 4, IntPtr.Zero); ReadProcessMemory(gameProc.Handle, MAX_EXTRA_EMBLEMS_ADDRESS, maxExtraBuffer, 4, IntPtr.Zero); maxEmblems = BC.ToInt32(maxEmblemsBuffer, 0); maxExtra = BC.ToInt32(maxExtraBuffer, 0); if (maxEmblems + maxExtra != 0) { break; } Thread.Sleep(2000); tries++; }while (tries < 5); Emblems = new Emblem[maxEmblems]; ExtraEmblems = new ExtraEmblem[maxExtra]; byte[] currentEmblem = new byte[128]; //size of 1 emblem object in memory int address = EMBLEMS_ADDRESS; for (int i = 0; i < Emblems.Length; i++) { ReadProcessMemory(gameProc.Handle, address, currentEmblem, currentEmblem.Length, IntPtr.Zero); //WriteProcessMemory(gameProc.Handle, address + 4, new byte[1] { 1 }, 1, IntPtr.Zero); //for the fun stuff Emblems[i] = new Emblem() { type = currentEmblem[0], tag = BC.ToInt16(currentEmblem, 2), level = BC.ToInt16(currentEmblem, 4), sprite = Convert.ToChar(currentEmblem[6]), color = BC.ToUInt16(currentEmblem, 8), var = BC.ToInt32(currentEmblem, 12), hint = Encoding.ASCII.GetString(currentEmblem, 16, 110), collected = currentEmblem[126] }; address += 128; } currentEmblem = new byte[68]; //size of 1 extra emblem object in memory address = EXTRA_EMBLEMS_ADDRESS; for (int i = 0; i < ExtraEmblems.Length; i++) { ReadProcessMemory(gameProc.Handle, address, currentEmblem, currentEmblem.Length, IntPtr.Zero); ExtraEmblems[i] = new ExtraEmblem() { name = Encoding.ASCII.GetString(currentEmblem, 0, 20), description = Encoding.ASCII.GetString(currentEmblem, 20, 40), conditionset = currentEmblem[60], showconditionset = currentEmblem[61], sprite = Convert.ToChar(currentEmblem[62]), color = BC.ToUInt16(currentEmblem, 64), collected = currentEmblem[66], }; address += 68; } if (!(previousEmblems.SequenceEqual(Emblems) && previousExtraEmblems.SequenceEqual(ExtraEmblems))) { EmblemsChangedEvent(this, EmblemsChangedEventArgs.FullInfo); } previousEmblems = (Emblem[])Emblems.Clone(); previousExtraEmblems = (ExtraEmblem[])ExtraEmblems.Clone(); }
/// <summary> /// Returns a half-precision floating point number converted from two bytes /// at a specified position in a byte array. /// </summary> /// <param name="value">An array of bytes.</param> /// <param name="startIndex">The starting position within value.</param> /// <returns>A half-precision floating point number formed by two bytes beginning at startIndex.</returns> /// <exception cref="System.ArgumentException"> /// startIndex is greater than or equal to the length of value minus 1, and is /// less than or equal to the length of value minus 1. /// </exception> /// <exception cref="System.ArgumentNullException">value is null.</exception> /// <exception cref="System.ArgumentOutOfRangeException">startIndex is less than zero or greater than the length of value minus 1.</exception> public static Half ToHalf(byte[] value, int startIndex) { return(ToHalf((ushort)BitConverter.ToInt16(value, startIndex))); }
private static void ParseRegTzi(List <AdjustmentRule> adjustmentRules, int start_year, int end_year, byte [] buffer) { //int standard_bias = BitConverter.ToInt32 (buffer, 4); /* not sure how to handle this */ int daylight_bias = BitConverter.ToInt32(buffer, 8); int standard_year = BitConverter.ToInt16(buffer, 12); int standard_month = BitConverter.ToInt16(buffer, 14); int standard_dayofweek = BitConverter.ToInt16(buffer, 16); int standard_day = BitConverter.ToInt16(buffer, 18); int standard_hour = BitConverter.ToInt16(buffer, 20); int standard_minute = BitConverter.ToInt16(buffer, 22); int standard_second = BitConverter.ToInt16(buffer, 24); int standard_millisecond = BitConverter.ToInt16(buffer, 26); int daylight_year = BitConverter.ToInt16(buffer, 28); int daylight_month = BitConverter.ToInt16(buffer, 30); int daylight_dayofweek = BitConverter.ToInt16(buffer, 32); int daylight_day = BitConverter.ToInt16(buffer, 34); int daylight_hour = BitConverter.ToInt16(buffer, 36); int daylight_minute = BitConverter.ToInt16(buffer, 38); int daylight_second = BitConverter.ToInt16(buffer, 40); int daylight_millisecond = BitConverter.ToInt16(buffer, 42); if (standard_month == 0 || daylight_month == 0) { return; } DateTime start_date; DateTime start_timeofday = new DateTime(1, 1, 1, daylight_hour, daylight_minute, daylight_second, daylight_millisecond); TransitionTime start_transition_time; if (daylight_year == 0) { start_date = new DateTime(start_year, 1, 1); start_transition_time = TransitionTime.CreateFloatingDateRule( start_timeofday, daylight_month, daylight_day, (DayOfWeek)daylight_dayofweek); } else { start_date = new DateTime(daylight_year, daylight_month, daylight_day, daylight_hour, daylight_minute, daylight_second, daylight_millisecond); start_transition_time = TransitionTime.CreateFixedDateRule( start_timeofday, daylight_month, daylight_day); } DateTime end_date; DateTime end_timeofday = new DateTime(1, 1, 1, standard_hour, standard_minute, standard_second, standard_millisecond); TransitionTime end_transition_time; if (standard_year == 0) { end_date = new DateTime(end_year, 12, 31); end_transition_time = TransitionTime.CreateFloatingDateRule( end_timeofday, standard_month, standard_day, (DayOfWeek)standard_dayofweek); } else { end_date = new DateTime(standard_year, standard_month, standard_day, standard_hour, standard_minute, standard_second, standard_millisecond); end_transition_time = TransitionTime.CreateFixedDateRule( end_timeofday, standard_month, standard_day); } TimeSpan daylight_delta = new TimeSpan(0, -daylight_bias, 0); adjustmentRules.Add(AdjustmentRule.CreateAdjustmentRule( start_date, end_date, daylight_delta, start_transition_time, end_transition_time)); }
/// <summary> /// 返回指定的字节数组中以指定位置的字节转换来的值 /// </summary> /// <param name="buffer">来源字节数组</param> /// <param name="offset">数据偏移</param> /// <returns>值</returns> public static short ToShort(this byte[] buffer, int offset) { return BitConverter.ToInt16(buffer, offset); }
/// <summary> /// 从流中读入一个 <see cref="T:System.Int16"/> /// </summary> /// <param name="stream">要读取的流</param> /// <returns>读取的 <see cref="T:System.Int16"/></returns> public static short ReadInt16(this System.IO.Stream stream) { return(BitConverter.ToInt16(stream.ReadBuffer(2), 0)); }
public static short ToInt16(ReadOnlySpan <byte> value) => Converter.ToInt16(value);
/// <summary>转为浮点数</summary> /// <param name="value">待转换对象</param> /// <param name="defaultValue">默认值。待转换对象无效时使用</param> /// <returns></returns> public virtual Double ToDouble(Object value, Double defaultValue) { if (value == null || value == DBNull.Value) { return(defaultValue); } // 特殊处理字符串,也是最常见的 if (value is String str) { str = ToDBC(str).Trim(); if (str.IsNullOrEmpty()) { return(defaultValue); } if (Double.TryParse(str, out var n)) { return(n); } return(defaultValue); } else if (value is Byte[] buf) { if (buf == null || buf.Length < 1) { return(defaultValue); } switch (buf.Length) { case 1: return(buf[0]); case 2: return(BitConverter.ToInt16(buf, 0)); case 3: return(BitConverter.ToInt32(new Byte[] { buf[0], buf[1], buf[2], 0 }, 0)); case 4: return(BitConverter.ToInt32(buf, 0)); default: // 凑够8字节 if (buf.Length < 8) { var bts = new Byte[8]; Buffer.BlockCopy(buf, 0, bts, 0, buf.Length); buf = bts; } return(BitConverter.ToDouble(buf, 0)); } } try { return(Convert.ToDouble(value)); } catch { return(defaultValue); } }
/// <summary>转为长整数。支持字符串、全角、字节数组(小端)、时间(Unix毫秒)</summary> /// <param name="value">待转换对象</param> /// <param name="defaultValue">默认值。待转换对象无效时使用</param> /// <returns></returns> public virtual Int64 ToLong(Object value, Int64 defaultValue) { if (value == null || value == DBNull.Value) { return(defaultValue); } // 特殊处理字符串,也是最常见的 if (value is String str) { // 拷贝而来的逗号分隔整数 str = str.Replace(",", null); str = ToDBC(str).Trim(); if (str.IsNullOrEmpty()) { return(defaultValue); } if (Int64.TryParse(str, out var n)) { return(n); } return(defaultValue); } // 特殊处理时间,转Unix毫秒 if (value is DateTime dt) { if (dt == DateTime.MinValue) { return(0); } //// 先转UTC时间再相减,以得到绝对时间差 //return (Int32)(dt.ToUniversalTime() - _dt1970).TotalSeconds; return((Int64)(dt - _dt1970).TotalMilliseconds); } if (value is DateTimeOffset dto) { if (dto == DateTimeOffset.MinValue) { return(0); } return((Int64)(dto - _dto1970).TotalMilliseconds); } if (value is Byte[] buf) { if (buf == null || buf.Length < 1) { return(defaultValue); } switch (buf.Length) { case 1: return(buf[0]); case 2: return(BitConverter.ToInt16(buf, 0)); case 3: return(BitConverter.ToInt32(new Byte[] { buf[0], buf[1], buf[2], 0 }, 0)); case 4: return(BitConverter.ToInt32(buf, 0)); case 8: return(BitConverter.ToInt64(buf, 0)); default: break; } } //暂时不做处理 先处理异常转换 try { return(Convert.ToInt64(value)); } catch { return(defaultValue); } }