Click here to Skip to main content
15,915,093 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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;
        }


What I have tried:

C#
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;
		}

	}
Posted
Updated 12-Jul-18 0:14am
v2
Comments
F-ES Sitecore 12-Jul-18 6:04am    
What line does the error occur on? You'll get this if you try and retrieve an item from a dictionary using a key that doesn't exist in that dictionary. For example;

Dictionary<int, FieldParseInfo> guide = new Dictionary<int, FieldParseInfo>();
....
FieldParseInfo fpi = guide[i];

"guide" is still an empty dictionary so it can't possibly contain something with the key of "i".
Shahbaz435 12-Jul-18 6:10am    
yes guide is null so how should i get the exact solution for this plz help im stuck in this code since morning

1 solution

We can;t solve this for you. The error is very explicit:
Quote:
The given key was not present in the dictionary
Means what it says - you have tried to read a value out of a Dictionary that you didn't put in there:
C#
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("John", "Smith");
dict.Add("Mike", "Hunt");
Console.WriteLine(dict["Joe"]);
So use the debugger.
Put a breakpoint on the line that causes the exception, and look at the index value you are trying to access. Then look at the Dictionary itself, and see what values you do have in there. Should the index be there? If not, why not? If it should, then why wasn't it added?

We can't do that for you - we can't run your code, and don't have access to your data - so give it a try, and see what information you can find out.
 
Share this answer
 
Comments
Shahbaz435 12-Jul-18 6:18am    
im new to the dictionary and im getting the error on
Dictionary<int, FieldParseInfo> parseMap = new Dictionary<int, FieldParseInfo>();
//Dictionary<int, FieldParseInfo> guide = parseMap[type];

List<int> index = parseOrder[type]; this line. and parsemap should be null how to fetch the data on parsemap in the dictionaryy plz tell me
OriginalGriff 12-Jul-18 6:22am    
You can't access anything from an empty dictionary.
Go back to wherever you got that code from and look / ask there - we have no idea what your "parsemap" is or what it is supposed to hold, let alone how you get information into it!
Shahbaz435 12-Jul-18 6:24am    
parsemap is the name im giving 1st it was guide then i ll replace to parsemap plz help me sir
OriginalGriff 12-Jul-18 6:32am    
The name is irrelevant. What matters is that you need to get some data into it before you can use it.

As I said: go back to where you got your code from, and look there.
Shahbaz435 12-Jul-18 6:39am    
can u plz help me i have given the code plz help sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900