123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- using Edge.Core.Parser.BinaryParser.Util;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ShellChina_EPS_Project_CarPlatePay_EpsClient_App.MessageEntity
- {
- public static class BitMapHelper
- {
- /// <summary>
- /// create 8 bytes map.
- /// </summary>
- /// <returns></returns>
- public static byte[] CreateMap()
- {
- byte[] map = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
- return map;
- }
- /// <summary>
- /// create 8 bytes map with fields enabled(correlated bit set to 1)
- /// </summary>
- /// <param name="enablingFieldIndexes">1 based, means the first field's index is 1</param>
- /// <returns></returns>
- public static byte[] CreateMap(params int[] enablingFieldIndexes)
- {
- if (enablingFieldIndexes.Any(i => i < 1)) throw new ArgumentOutOfRangeException("fields' index must >=1");
- byte[] map = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
- return map.Enable(enablingFieldIndexes);
- }
- public static byte[] Enable(this byte[] map, params int[] enablingFieldIndexes)
- {
- for (int i = 0; i < enablingFieldIndexes.Length; i++)
- {
- var fieldIndex = enablingFieldIndexes[i] - 1;
- var groupIndex = fieldIndex / 8;
- var groupInternalIndex = fieldIndex % 8;
- map[groupIndex] = map[groupIndex].SetBit(7 - groupInternalIndex, 7 - groupInternalIndex, 1);
- }
- return map;
- }
- public static byte[] Disable(this byte[] map, params int[] disablingFieldIndexes)
- {
- for (int i = 0; i < disablingFieldIndexes.Length; i++)
- {
- var fieldIndex = disablingFieldIndexes[i] - 1;
- var groupIndex = fieldIndex / 8;
- var groupInternalIndex = fieldIndex % 8;
- map[groupIndex] = map[groupIndex].SetBit(7 - groupInternalIndex, 7 - groupInternalIndex, 0);
- }
- return map;
- }
- /// <summary>
- /// check if target field presents in map
- /// </summary>
- /// <param name="map"></param>
- /// <param name="fieldIndex">0 based</param>
- /// <returns>true for present, otherwise, ommit</returns>
- public static bool Determine(this IList<byte> map, int fieldIndex)
- {
- var groupIndex = (fieldIndex - 1) / 8;
- var groupInternalIndex = (fieldIndex - 1) % 8;
- return map[groupIndex].GetBit(7 - groupInternalIndex) == 1;
- }
- public static List<int> Determine(this IList<byte> map)
- {
- var result = new List<int>();
- for (int i = 0; i < map.Count; i++)
- {
- for (int j = 7; j >= 0; j--)
- {
- if (map[i].GetBit(j) == 1)
- result.Add(i * 8 + 7 - j + 1);
- }
- }
- return result;
- }
- }
- }
|