/// <summary> /// Parses a value of the type and length specified in the constructor /// and returns the IsoValue. /// </summary> /// <param name="buf">The byte buffer containing the value to parse.</param> /// <param name="pos">The position inside the byte buffer where the parsing must start.</param> /// <param name="encoder">The encoder to use for converting bytes to strings.</param> /// <returns>The resulting IsoValue with the given types and length, and the stored value.</returns> public IsoValue Parse(CaseBusiness.ISO.EncodingType encoding, Byte[] buf, Int32 pos, Encoding encoder, Boolean isSub) { try { if (encoding == CaseBusiness.ISO.EncodingType.EBCDIC && Base == ConfigParser.Base.Hexadecimal) { Byte[] bufTemp = null; String fieldASCII = ""; String result = ""; Int32 lengthLenght = 0; if (type == IsoType.LLVAR) { bufTemp = new Byte[2]; Array.Copy(buf, pos, bufTemp, 0, 2); fieldASCII = ParseCharacter.convertEBCDIC2ASCII(bufTemp); bufTemp = System.Text.Encoding.ASCII.GetBytes(fieldASCII); length = ((bufTemp[0] - 48) * 10) + (bufTemp[1] - 48); lengthLenght = 2; } if (type == IsoType.LLLVAR) { bufTemp = new Byte[3]; Array.Copy(buf, pos, bufTemp, 0, 3); fieldASCII = ParseCharacter.convertEBCDIC2ASCII(bufTemp); bufTemp = System.Text.Encoding.ASCII.GetBytes(fieldASCII); length = ((bufTemp[0] - 48) * 100) + ((bufTemp[1] - 48) * 10) + (bufTemp[2] - 48); lengthLenght = 3; } for (int i = 0; i < length; i++) { result += buf[pos + lengthLenght + i].ToString("X").PadLeft(2, '0'); } return(new IsoValue(type, Base, result, result)); } if (Base == ConfigParser.Base.B64) { buf = Encoding.ASCII.GetBytes(Convert.ToBase64String(buf)); } if (type == IsoType.NUMERIC || type == IsoType.ALPHA) { Int32 Llength = length; if (isSub) { if ((pos + length) > buf.Length || length == 0) { Llength = buf.Length - pos; } } if (encoding == CaseBusiness.ISO.EncodingType.EBCDIC) { Byte[] bufTemp = new Byte[length]; String fieldASCII = ""; Array.Copy(buf, pos, bufTemp, 0, length); fieldASCII = ParseCharacter.convertEBCDIC2ASCII(bufTemp); pos = 0; buf = System.Text.Encoding.ASCII.GetBytes(fieldASCII); } else if (Base == ConfigParser.Base.Hexadecimal) { String result = ""; for (int i = 0; i < Llength / 2; i++) { result += buf[pos + i].ToString("X").PadLeft(2, '0'); } pos = 0; buf = System.Text.Encoding.ASCII.GetBytes(result); } return(new IsoValue(type, Base, encoder.GetString(buf, pos, Llength), Llength, encoder.GetString(buf, pos, Llength), typeElement)); } if (type == IsoType.LLVAR) { if (encoding == CaseBusiness.ISO.EncodingType.EBCDIC) { Byte[] bufTemp = new Byte[2]; String fieldASCII = ""; Array.Copy(buf, pos, bufTemp, 0, 2); fieldASCII = ParseCharacter.convertEBCDIC2ASCII(bufTemp); bufTemp = System.Text.Encoding.ASCII.GetBytes(fieldASCII); length = ((bufTemp[0] - 48) * 10) + (bufTemp[1] - 48); Array.Resize(ref bufTemp, length); Array.Copy(buf, pos + 2, bufTemp, 0, length); fieldASCII += ParseCharacter.convertEBCDIC2ASCII(bufTemp); pos = 0; buf = System.Text.Encoding.ASCII.GetBytes(fieldASCII); } 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, Base, encoder.GetString(buf, pos + 2, length), encoder.GetString(buf, pos + 2, length))); } if (type == IsoType.LLLVAR) { if (encoding == CaseBusiness.ISO.EncodingType.EBCDIC) { Byte[] bufTemp = new Byte[3]; String fieldASCII = ""; Array.Copy(buf, pos, bufTemp, 0, 3); fieldASCII = ParseCharacter.convertEBCDIC2ASCII(bufTemp); bufTemp = System.Text.Encoding.ASCII.GetBytes(fieldASCII); length = ((bufTemp[0] - 48) * 100) + ((bufTemp[1] - 48) * 10) + (bufTemp[2] - 48); Array.Resize(ref bufTemp, length); Array.Copy(buf, pos + 3, bufTemp, 0, length); fieldASCII += ParseCharacter.convertEBCDIC2ASCII(bufTemp); pos = 0; buf = System.Text.Encoding.ASCII.GetBytes(fieldASCII); } 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, Base, encoder.GetString(buf, pos + 3, length), encoder.GetString(buf, pos + 3, length))); } if (type == IsoType.AMOUNT) { if (encoding == CaseBusiness.ISO.EncodingType.EBCDIC) { Byte[] bufTemp = new Byte[12]; String fieldASCII = ""; Array.Copy(buf, pos, bufTemp, 0, 12); fieldASCII = ParseCharacter.convertEBCDIC2ASCII(bufTemp); pos = 0; buf = System.Text.Encoding.ASCII.GetBytes(fieldASCII); } 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, Base, Decimal.Parse(encoder.GetString(c)), encoder.GetString(buf, pos, 12))); } if (type == IsoType.DATE10) { DateTime dt = DateTime.Now; if (encoding == CaseBusiness.ISO.EncodingType.EBCDIC) { Byte[] bufTemp = new Byte[10]; String fieldASCII = ""; Array.Copy(buf, pos, bufTemp, 0, 10); fieldASCII = ParseCharacter.convertEBCDIC2ASCII(bufTemp); pos = 0; buf = System.Text.Encoding.ASCII.GetBytes(fieldASCII); } 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.Month > DateTime.Now.Month) { dt = dt.AddYears(-1); } return(new IsoValue(type, Base, dt, encoder.GetString(buf, pos, 10))); } if (type == IsoType.DATE12) { DateTime dt = DateTime.Now; if (encoding == CaseBusiness.ISO.EncodingType.EBCDIC) { Byte[] bufTemp = new Byte[12]; String fieldASCII = ""; Array.Copy(buf, pos, bufTemp, 0, 12); fieldASCII = ParseCharacter.convertEBCDIC2ASCII(bufTemp); pos = 0; buf = System.Text.Encoding.ASCII.GetBytes(fieldASCII); } dt = new DateTime( (((buf[pos + 2] - 48) * 10) + buf[pos + 3] - 48) <= DateTime.Now.Month ? DateTime.Now.Year : DateTime.Now.Year - 1, ((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, ((buf[pos + 10] - 48) * 10) + buf[pos + 11] - 48, 0); return(new IsoValue(type, Base, dt, encoder.GetString(buf, pos, 12))); } if (type == IsoType.DATE6) { DateTime dt = DateTime.Now; if (encoding == CaseBusiness.ISO.EncodingType.EBCDIC) { Byte[] bufTemp = new Byte[6]; String fieldASCII = ""; Array.Copy(buf, pos, bufTemp, 0, 6); fieldASCII = ParseCharacter.convertEBCDIC2ASCII(bufTemp); pos = 0; buf = System.Text.Encoding.ASCII.GetBytes(fieldASCII); } dt = new DateTime( ((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, Base, dt, encoder.GetString(buf, pos, 6))); } if (type == IsoType.DATE4) { DateTime dt = DateTime.Now; if (encoding == CaseBusiness.ISO.EncodingType.EBCDIC) { Byte[] bufTemp = new Byte[4]; String fieldASCII = ""; Array.Copy(buf, pos, bufTemp, 0, 4); fieldASCII = ParseCharacter.convertEBCDIC2ASCII(bufTemp); pos = 0; buf = System.Text.Encoding.ASCII.GetBytes(fieldASCII); } dt = new DateTime(dt.Year, ((buf[pos] - 48) * 10) + buf[pos + 1] - 48, ((buf[pos + 2] - 48) * 10) + buf[pos + 3] - 48); if (dt.Month > DateTime.Now.Month) { dt = dt.AddYears(-1); } return(new IsoValue(type, Base, dt, encoder.GetString(buf, pos, 4))); } if (type == IsoType.DATE_EXP) { DateTime dt = DateTime.Now; if (encoding == CaseBusiness.ISO.EncodingType.EBCDIC) { Byte[] bufTemp = new Byte[4]; String fieldASCII = ""; Array.Copy(buf, pos, bufTemp, 0, 4); fieldASCII = ParseCharacter.convertEBCDIC2ASCII(bufTemp); pos = 0; buf = System.Text.Encoding.ASCII.GetBytes(fieldASCII); } 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); dt = dt.AddMonths(1); dt = dt.AddDays(-1); return(new IsoValue(type, Base, dt, encoder.GetString(buf, pos, 4))); } if (type == IsoType.TIME) { DateTime dt = DateTime.Now; if (encoding == CaseBusiness.ISO.EncodingType.EBCDIC) { Byte[] bufTemp = new Byte[6]; String fieldASCII = ""; Array.Copy(buf, pos, bufTemp, 0, 6); fieldASCII = ParseCharacter.convertEBCDIC2ASCII(bufTemp); pos = 0; buf = System.Text.Encoding.ASCII.GetBytes(fieldASCII); } 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, Base, dt, encoder.GetString(buf, pos, 6))); } return(new IsoValue()); } catch { return(new IsoValue()); } }
/// <summary> /// Parse and store the data in its respective subelement/subfield /// </summary> /// <param name="configBase"></param> /// <param name="subDataElements_Fields"></param> /// <param name="val"></param> /// <param name="encoder"></param> /// <param name="pos"></param> /// <returns></returns> internal IsoValue parseDataElement_Field(CaseBusiness.ISO.EncodingType encoding, ConfigParser.Base configBase, Dictionary <Int32, FieldParseInfo> subDataElements_Fields, IsoValue val, Encoding encoder, ref Int32 pos) { Byte[] bufSub = null; if (configBase == ConfigParser.Base.B64) { bufSub = Convert.FromBase64String(val.OriginalValue); } else { bufSub = Encoding.ASCII.GetBytes(val.OriginalValue); } Int32 posLeitura = 0; Int32 multFieldHex = 1; //variavel para controle de campos do tipo Binary (recebido no formato Hex) Int32 multHex = 1; //variavel para controle de campos do tipo Binary (recebido no formato Hex) Int32 DE_FieldsChecked = 0; for (Int32 i = 0; DE_FieldsChecked < subDataElements_Fields.Count; i++) { Int32 posSub = 0; KeyValuePair <Int32, FieldParseInfo> subData = subDataElements_Fields.ElementAt(i); if (val.SubData.Keys.Contains(subData.Key)) { continue; } if (posLeitura > 0) { posSub = posLeitura; } if (subData.Value.TypeElement == TypeElement.SubElement) { #region SubElement Int32 numSubElementArray = 0; if (configBase == ConfigParser.Base.Hexadecimal) //Quando campo Binario, й tratado como campo Hexadecimal, logo a extraзгo de bytes й dobrada { multFieldHex = 2; multHex = 2; numSubElementArray = Convert.ToInt32(Encoding.ASCII.GetString(bufSub, posSub, subData.Value.SubElementIDLength * multFieldHex), 16); } else { multFieldHex = 1; multHex = 1; numSubElementArray = Convert.ToInt32(Encoding.ASCII.GetString(bufSub, posSub, subData.Value.SubElementIDLength)); } if (numSubElementArray == subData.Key) { posSub += subData.Value.SubElementIDLength * multFieldHex; Int32 tamSubElement = (configBase == ConfigParser.Base.Hexadecimal ? Convert.ToInt32(Encoding.ASCII.GetString(bufSub, posSub, subData.Value.LengthOfLengthSubElement * multFieldHex), 16) : Convert.ToInt32(Encoding.ASCII.GetString(bufSub, posSub, subData.Value.LengthOfLengthSubElement * multFieldHex))) * multHex; posSub += subData.Value.LengthOfLengthSubElement * multFieldHex; String value = ""; IsoValue valSub = null; //Byte[] bufSubSub = null; if (configBase == ConfigParser.Base.B64) { value = Convert.ToBase64String(bufSub, posSub, tamSubElement); //bufSubSub = Encoding.ASCII.GetBytes(valSub.OriginalValue); } else { value = Encoding.ASCII.GetString(bufSub, posSub, tamSubElement); // bufSubSub = Encoding.ASCII.GetBytes(valSub.OriginalValue); } valSub = new IsoValue(subData.Value.Type, configBase, value, value.Length, value, "", subData.Value.TypeElement, subData.Value.SubElementIDLength, subData.Value.LengthOfLengthSubElement); if (subData.Value.SubDataElements_Fields == null || subData.Value.SubDataElements_Fields.Count == 0) { val.SubData.Add(subData.Key, valSub); } else //Quando: subData.Value.SubDataElements_Fields.Count > 0 { val.SubData.Add(subData.Key, parseDataElement_Field(encoding, configBase, subData.Value.SubDataElements_Fields, valSub, encoder, ref pos)); } posSub += tamSubElement; posLeitura = posSub; DE_FieldsChecked++; i = -1; } else if (i == (subDataElements_Fields.Count - 1)) { DE_FieldsChecked++; i = -1; } #endregion SubElement } else { #region SubField if (posSub < bufSub.Length) { IsoValue valSub = subData.Value.Parse(encoding, bufSub, posSub, encoder, true); if (subData.Value.SubDataElements_Fields == null || subData.Value.SubDataElements_Fields.Count == 0) { if (valSub.OriginalValue != null) { val.SubData.Add(subData.Key, valSub); posSub += valSub.Length; posLeitura += valSub.Length; if (valSub.Type == IsoType.LLVAR) { pos += 2; } else if (valSub.Type == IsoType.LLLVAR) { pos += 3; } } else { return(null); } } else { val.SubData.Add(subData.Key, parseDataElement_Field(encoding, configBase, subData.Value.SubDataElements_Fields, valSub, encoder, ref pos)); } } DE_FieldsChecked++; #endregion } if (posSub >= val.Length) { break; } } val.CompletelyParsed = true; return(val); }