Ejemplo n.º 1
0
            private static void pack(TLVElement element)
            {
                if (element.Value.Length > MaxLength)
                {
                    throw new Exception("值长度过长");
                }
                byte[] result = element.Value;
                if (element.mTLVDef != null)
                {
                    int totalLength = element.Value.Length;
                    if (element.mTLVDef.LengthType == TLVDef.LengthFormat.Fix && element.mTLVDef.Length > element.Value.Length)
                    {
                        totalLength = element.mTLVDef.Length;
                    }
                    else if (element.mTLVDef.ValueType == TLVDef.DataType.B && element.Value.Length % 2 != 0)
                    {
                        totalLength += 1;
                    }
                    if (totalLength != element.Value.Length)
                    {
                        char padChar = '\0';
                        switch (element.mTLVDef.Padchar)
                        {
                        case TLVDef.PadType.Space:
                            padChar = ' ';
                            break;

                        case TLVDef.PadType.Zero:
                            padChar = '0';
                            break;
                        }
                        string padStr = new string(padChar, totalLength - element.Value.Length);
                        result = new byte[totalLength];
                        if (element.mTLVDef.Align == TLVDef.AlignType.Left)
                        {
                            Array.Copy(element.Value, result, element.Value.Length);
                            Array.Copy(Encoding.Default.GetBytes(padStr), 0, result, element.Value.Length, padStr.Length);
                        }
                        else
                        {
                            Array.Copy(Encoding.Default.GetBytes(padStr), result, padStr.Length);
                            Array.Copy(element.Value, 0, result, padStr.Length, element.Value.Length);
                        }
                    }
                    if (element.mTLVDef.ValueType == TLVDef.DataType.B)
                    {
                        result = Utility.str2Bcd(Encoding.Default.GetString(result));
                    }
                }
                element.Value       = result;
                element.ValueLength = result.Length;
                element.TotalLength = element.Tag.Length + CalcLength(result) + element.ValueLength;
            }
Ejemplo n.º 2
0
        public void ParseTLV(byte[] content)
        {
            Dictionary <string, byte[]> result = new Dictionary <string, byte[]>();

            ParseTLV(content, result);
            mTags.Clear();
            foreach (KeyValuePair <string, byte[]> tmp in result)
            {
                TLVElement element = new TLVElement(tmp.Key, tmp.Value, getTLVDefByTag(tmp.Key), false);
                element.UnPack();
                mTags.Add(tmp.Key, element);
            }
        }
Ejemplo n.º 3
0
 private static void unPack(TLVElement element)
 {
     if (element.Value.Length > MaxLength)
     {
         throw new Exception("值长度过长");
     }
     byte[] result = element.Value;
     if (element.mTLVDef != null)
     {
         if (element.mTLVDef.ValueType == TLVDef.DataType.B)
         {
             result = Encoding.Default.GetBytes(Utility.bcd2str(element.Value, element.Value.Length));
         }
     }
     element.Value       = result;
     element.ValueLength = result.Length;
     element.TotalLength = element.Tag.Length + CalcLength(result) + element.ValueLength;
 }