Ejemplo n.º 1
0
 /// <summary>
 /// Gets combined hash of underlying <see langword="string"/> and its <see cref="Encoding"/>.
 /// </summary>
 /// <returns>Combined hash of underlying <see langword="string"/> and recognized <see cref="Encoding"/></returns>
 public override int GetHashCode() => HashCode.Combine(Value.GetHashCode(), UsedEncoding.GetHashCode()); // Strings might be the same, but with different encoding? not sure if I should differentiate that
Ejemplo n.º 2
0
        private string ConvertToText(byte[] message, bool request)
        {
            string text = "";

            switch (PacketView)
            {
            case ePacketView.String:
                // ----- Remove zero chars -----
                var messageList = message.ToList();
                for (int i = messageList.Count - 1; i >= 0; i--)
                {
                    if (messageList[i] == 0)
                    {
                        messageList.RemoveAt(i);
                    }
                }
                var newMessage = messageList.ToArray();
                text = UsedEncoding.GetString(newMessage);

                // ----- Split message if defined separator char -----
                if (LineSeparatingChar != "")
                {
                    text = text.Replace(LineSeparatingChar, Environment.NewLine);
                }

                text = text.Trim();

                break;

            case ePacketView.StringReplaceCommandChars:
                // ----- Create zero mask -----
                byte[] newMsg = new byte[message.Length];
                Array.Copy(message, newMsg, message.Length);

                // ----- Replace zero chars -----
                for (int i = 0; i < newMsg.Length; i++)
                {
                    if (newMsg[i] == 0)
                    {
                        newMsg[i] = 1;
                    }
                }
                text = UsedEncoding.GetString(newMsg);
                var byteText = UsedEncoding.GetBytes(text);

                if (text.Length == newMsg.Length)
                {
                    // ----- Replace special chars -----
                    for (int i = text.Length - 1; i >= 0; i--)
                    {
                        // ----- If command char ----
                        if (byteText[i] < 32)
                        {
                            // ----- And not endline or tabulator -----
                            if (byteText[i] != 10 && byteText[i] != 10 && byteText[i] != 13)
                            {
                                text = text.Remove(i, 1);
                                if (message[i] == 0)
                                {
                                    text = text.Insert(i, "{0}");
                                }
                                else
                                {
                                    text = text.Insert(i, "{" + byteText[i].ToString() + "}");
                                }
                            }
                        }
                    }
                }

                // ----- Split message if defined separator char -----
                if (LineSeparatingChar != "")
                {
                    text = text.Replace(LineSeparatingChar, Environment.NewLine);
                }

                text = text.Trim();

                break;

            case ePacketView.Bytes:
                for (int i = 0; i < message.Length; i++)
                {
                    text += @"\" + message[i].ToString();
                }
                break;

            case ePacketView.Hex:
                for (int i = 0; i < message.Length; i++)
                {
                    if (text != "")
                    {
                        text += " ";
                    }
                    text += message[i].ToString("X2");
                }
                break;

            case ePacketView.Custom:
                if (CustomView != null)
                {
                    text = CustomView.ParsePacket(message, request);
                }
                break;
            }


            return(text);
        }