Shahbaz Kaware

Shahbaz Kaware

  • NA
  • 535
  • 31.4k

The given key was not present in the dictionary this error

Jul 12 2018 7:20 AM
private Dictionary<int, Dictionary<int, FieldParseInfo>> parseMap = new Dictionary<int, Dictionary<int, FieldParseInfo>>(); public void SetParseDictionary(int type, Dictionary<int, FieldParseInfo> dict) { parseMap[type] = dict; List<int> index = new List<int>(); ///Agregar todas las llaves del dict a index ///ordenar index por numeros for (int i = 2; i < 129; i++) { if (dict.ContainsKey(i)) { index.Add(i); } } parseOrder[type] = index; } public IsoMessage ParseMessage(byte[] buf, int isoHeaderLength, Encoding encoder) { IsoMessage m = new IsoMessage(isoHeaderLength > 0 ? encoder.GetString(buf, 0, isoHeaderLength) : null); int type = ((buf[isoHeaderLength] - 48) << 12) | ((buf[isoHeaderLength + 1] - 48) << 8) | ((buf[isoHeaderLength + 2] - 48) << 4) | (buf[isoHeaderLength + 3] - 48); m.Type = type; //Parse the bitmap bool extended = (HexByteValue(buf[isoHeaderLength + 4]) & 8) > 0; BitArray bs = new BitArray(extended ? 128 : 64); int pos = 0; for (int i = isoHeaderLength + 4; i < isoHeaderLength + 20; i++) { int hex = HexByteValue(buf[i]); bs.Set(pos++, (hex & 8) > 0); bs.Set(pos++, (hex & 4) > 0); bs.Set(pos++, (hex & 2) > 0); bs.Set(pos++, (hex & 1) > 0); } //Extended bitmap? if (bs.Get(0)) { for (int i = isoHeaderLength + 20; i < isoHeaderLength + 36; i++) { int hex = HexByteValue(buf[i]); bs.Set(pos++, (hex & 8) > 0); bs.Set(pos++, (hex & 4) > 0); bs.Set(pos++, (hex & 2) > 0); bs.Set(pos++, (hex & 1) > 0); } pos = 36 + isoHeaderLength; } else { pos = 20 + isoHeaderLength; } //////Parse each field //Dictionary<int, FieldParseInfo> guide = parseMap[type]; Dictionary<int, FieldParseInfo> guide = new Dictionary<int, FieldParseInfo>(); //Dictionary<int, FieldParseInfo> guide = parseMap[type]; //Dictionary<int, Dictionary<int, FieldParseInfo>> guide = new Dictionary<int, Dictionary<int, FieldParseInfo>>(); List<int> index = parseOrder[type]; try { foreach (int i in index) { FieldParseInfo fpi = guide[i]; if (bs.Get(i - 1)) { IsoValue val = fpi.Parse(buf, pos, encoder); m.SetField(i, val); pos += val.Length; if (val.Type == IsoType.LLVAR) { pos += 2; } else if (val.Type == IsoType.LLLVAR) { pos += 3; } } } } catch { } return m; }
 
 
public class FieldParseInfo { 		private IsoType type; 		private int length;         //private int parseOrder;  		/// <summary> 		/// Creates a new instance that knows how to parse a value of the given 		/// type and the given length (the length is necessary for ALPHA and NUMERIC 		/// values only). 		/// 		/// <param name="t">The ISO8583 type. 		/// <param name="len">The length of the value to parse (for ALPHA and NUMERIC values). 		public FieldParseInfo(IsoType t, int len)  // , int PO         {               type = t; 			length = len;             //parseOrder = PO; 		}  		/// <summary> 		/// The field length to parse. 		/// 		public int Length { 			get { return length; } 		}  		/// <summary> 		/// The type of the value that will be parsed. 		/// 		public IsoType Type { 			get { return type; } 		}          //public int ParseOrder         //{         //    get { return parseOrder; }         //}  		/// <summary> 		/// Parses a value of the type and length specified in the constructor 		/// and returns the IsoValue. 		/// 		/// <param name="buf">The byte buffer containing the value to parse. 		/// <param name="pos">The position inside the byte buffer where the parsing must start. 		/// <param name="encoder">The encoder to use for converting bytes to strings. 		/// <returns>The resulting IsoValue with the given types and length, and the stored value. 		public IsoValue Parse(byte[] buf, int pos, Encoding encoder)         { 			if (type == IsoType.NUMERIC || type == IsoType.ALPHA) { 				return new IsoValue(type, encoder.GetString(buf, pos, length), length); 			} else if (type == IsoType.LLVAR) { 				length = ((buf[pos] - 48) * 10) + (buf[pos + 1] - 48); 				if (length < 1 || length > 99) { 					throw new ArgumentException("LLVAR field with invalid length"); 				} 				return new IsoValue(type, encoder.GetString(buf, pos + 2, length)); 			} else if (type == IsoType.LLLVAR) { 				length = ((buf[pos] - 48) * 100) + ((buf[pos + 1] - 48) * 10) + (buf[pos + 2] - 48); 				if (length < 1 || length > 999) { 					throw new ArgumentException("LLLVAR field with invalid length"); 				} 				return new IsoValue(type, encoder.GetString(buf, pos + 3, length)); 			} else if (type == IsoType.AMOUNT) { 				byte[] c = new byte[13]; 				Array.Copy(buf, pos, c, 0, 10); 				Array.Copy(buf, pos + 10, c, 11, 2); 				c[10] = (byte)'.'; 				return new IsoValue(type, Decimal.Parse(encoder.GetString(c))); 			} else if (type == IsoType.DATE10) { 				DateTime dt = DateTime.Now; 				dt = new DateTime(dt.Year, 					((buf[pos] - 48) * 10) + buf[pos + 1] - 48, 					((buf[pos + 2] - 48) * 10) + buf[pos + 3] - 48, 					((buf[pos + 4] - 48) * 10) + buf[pos + 5] - 48, 					((buf[pos + 6] - 48) * 10) + buf[pos + 7] - 48, 					((buf[pos + 8] - 48) * 10) + buf[pos + 9] - 48); 				if (dt.CompareTo(DateTime.Now) > 0) { 					dt.AddYears(-1); 				} 				return new IsoValue(type, dt); 			} else if (type == IsoType.DATE4) { 				DateTime dt = DateTime.Now; 				dt = new DateTime(dt.Year, 					((buf[pos] - 48) * 10) + buf[pos + 1] - 48, 					((buf[pos + 2] - 48) * 10) + buf[pos + 3] - 48); 				if (dt.CompareTo(DateTime.Now) > 0) { 					dt.AddYears(-1); 				} 				return new IsoValue(type, dt); 			} else if (type == IsoType.DATE_EXP) { 				DateTime dt = DateTime.Now; 				dt = new DateTime(dt.Year - (dt.Year % 100) + ((buf[pos] - 48) * 10) + buf[pos + 1] - 48, 					((buf[pos + 2] - 48) * 10) + buf[pos + 3] - 48, 1); 				return new IsoValue(type, dt); 			} else if (type == IsoType.TIME) { 				DateTime dt = DateTime.Now; 				dt = new DateTime(dt.Year, dt.Month, dt.Day, 					((buf[pos] - 48) * 10) + buf[pos + 1] - 48, 					((buf[pos + 2] - 48) * 10) + buf[pos + 3] - 48, 					((buf[pos + 4] - 48) * 10) + buf[pos + 5] - 48); 				return new IsoValue(type, dt); 			} 			return null; 		}  	}
 


Answers (1)