示例#1
0
        /// <summary>
        /// Format the message for to send to the source
        /// </summary>
        /// <param name="mensagem">String to format</param>
        /// <returns>Array bytes</returns>
        public Byte[] FormatMessage(CaseBusiness.CC.Global.MensagemInterfaceDados interfaceDados, String mensagem)
        {
            Byte[] bytesMessage = new Byte[2];
            //Byte[] bytesTemp = null;
            Boolean secondBitMap = false;

            bytesMessage[0] = 0;
            bytesMessage[1] = 0;

            for (Int32 p = 0; p < mensagem.Length; p++)
            {
                //MTI
                if (p >= 0 && p < 4)
                {
                    Array.Resize(ref bytesMessage, bytesMessage.Length + 1);

                    if (interfaceDados.MensagemInterfaceHeader.TipoCodificacao == EncodingType.EBCDIC)
                    {
                        bytesMessage.SetValue(Convert.ToByte(ParseCharacter.convertASCII2EBCDIC(mensagem.Substring(p, 1))[0]), bytesMessage.Length - 1);
                    }
                    else
                    {
                        bytesMessage.SetValue(Convert.ToByte(mensagem.Substring(p, 1)[0]), bytesMessage.Length - 1);
                    }
                }

                //1є BitMap
                if (p >= 4 && p < 20)
                {
                    //Verifica se existe um 2є BitMap
                    if (p == 4)
                    {
                        Byte[] valor = Encoding.ASCII.GetBytes(mensagem.Substring(p, 1));
                        Int32  hex   = HexByteValue(valor[0]);
                        secondBitMap = (hex & 8) > 0;
                    }
                    Array.Resize(ref bytesMessage, bytesMessage.Length + 1);
                    bytesMessage.SetValue(Convert.ToByte(Convert.ToInt32(mensagem.Substring(p, 2), 16)), bytesMessage.Length - 1);
                    p++;
                }

                //2є BitMap ou DEs
                if (p >= 20)
                {
                    //Leitura 2є BitMap
                    if (secondBitMap && p <= 35)
                    {
                        Array.Resize(ref bytesMessage, bytesMessage.Length + 1);
                        bytesMessage.SetValue(Convert.ToByte(Convert.ToInt32(mensagem.Substring(p, 2), 16)), bytesMessage.Length - 1);
                        p++;
                    }

                    //Leitura DEs
                    if (!secondBitMap || p > 35)
                    {
                        Array.Resize(ref bytesMessage, bytesMessage.Length + 1);
                        if (interfaceDados.MensagemInterfaceHeader.TipoCodificacao == EncodingType.EBCDIC)
                        {
                            bytesMessage.SetValue(Convert.ToByte(ParseCharacter.convertASCII2EBCDIC(mensagem.Substring(p, 1))[0]), bytesMessage.Length - 1);
                        }
                        else
                        {
                            bytesMessage.SetValue(Convert.ToByte(mensagem.Substring(p, 1)[0]), bytesMessage.Length - 1);
                        }
                    }
                }
            }

            //Seta o tamanho da mensagem
            Int32  l = bytesMessage.Length - 2;
            String preMessageLength = l.ToString("X").PadLeft(4, '0');

            bytesMessage[0] = Convert.ToByte(Convert.ToInt32(preMessageLength.Substring(0, 2), 16));
            bytesMessage[1] = Convert.ToByte(Convert.ToInt32(preMessageLength.Substring(2, 2), 16));

            return(bytesMessage);
        }
示例#2
0
        /// <summary>
        /// Parses a buffer containing a message, considering the specified
        /// length as a prefix for the ISO header, using UTF8 encoding.
        /// </summary>
        /// <param name="buf">The buffer containing the message.</param>
        /// <param name="isoHeaderLength">The length of the ISO header.</param>
        /// <returns>The parsed message.</returns>
        public IsoMessage ParseMessage(CaseBusiness.CC.Global.MensagemInterfaceDados interfaceDados, Int32 isoHeaderLength, ParseMode parseMode)
        {
            Byte[] buf = interfaceDados.Dados;

            if (buf != null)
            {
                String  message      = "";
                Byte[]  messageData  = null;
                Boolean secondBitMap = false;
                Int32   fimConversao = 12;

                //MTI
                for (Int32 pos = 0; pos < fimConversao; pos++)
                {
                    if ((pos >= 0 && pos <= 3))
                    {
                        if (interfaceDados.MensagemInterfaceHeader.TipoCodificacao == EncodingType.EBCDIC)
                        {
                            message += ParseCharacter.convertEBCDIC2ASCII(new Byte[] { buf[pos] });
                        }
                        else
                        {
                            message += Encoding.ASCII.GetString(new Byte[] { buf[pos] });
                        }
                    }

                    //1є BitMap
                    if (pos > 3 && pos <= 11)
                    {
                        //Verifica se existe um 2є BitMap
                        if (pos == 4)
                        {
                            String caracter = "";
                            caracter = buf[pos].ToString("X").PadLeft(2, '0').Substring(0, 1);
                            Byte[] valor = Encoding.ASCII.GetBytes(caracter);
                            Int32  hex   = HexByteValue(valor[0]);
                            secondBitMap = (hex & 8) > 0;

                            if (secondBitMap)
                            {
                                fimConversao = 20;
                            }
                        }
                        message += buf[pos].ToString("X").PadLeft(2, '0');
                    }

                    //2є BitMap
                    if (pos > 11)
                    {
                        message += buf[pos].ToString("X").PadLeft(2, '0');
                    }
                }

                messageData = new Byte[buf.Length - fimConversao];
                Array.Copy(buf, fimConversao, messageData, 0, messageData.Length);

                //Converte string em array de bytes
                Byte[] mti_bitmap = Encoding.ASCII.GetBytes(message);
                return(ParseMessage(interfaceDados, mti_bitmap, messageData, isoHeaderLength, Encoding.UTF8, parseMode));
            }
            else
            {
                return(null);
            }
        }