Example #1
0
 internal static void AddNewMnemonic(string mnemonic, byte high, byte low)
 {
     ByteArrayEntry entry = new ByteArrayEntry(high, low);
     s_mnemonicToBits.Add(mnemonic, entry);
     s_bitsToMnemonic.Add(entry, mnemonic);
 }
Example #2
0
        /// <summary>Creates a list of <see cref="Token">Tokens</see> from an array of <see cref="byte">bytes</see>.
        /// </summary>
        /// <param name="bytes">Bytes.</param>
        /// <returns>A list of <see cref="Token">Tokens</see> created from the bytes.</returns>
        public static ReadOnlyCollection<Token> FromByteArray(byte[] bytes)
        {
            List<Token> tokens = new List<Token>();
            int index = 0;
            while(index < bytes.Length)
            {
                if (IsNumber(bytes[index]))
                {
                    int iVal = GetNumber(bytes, index);
                    tokens.Add(new Token(iVal.ToString(CultureInfo.InvariantCulture), bytes, index, 2));
                    index += 2;
                }
                else if ((TokenType)bytes[index] == TokenType.Text)
                {
                    int startIndex = index;
                    ArrayList charBytes = new ArrayList();
                    do
                    {
                        charBytes.Add(bytes[index+1]);
                        index += 2;
                    }
                    while((TokenType)bytes[index] == TokenType.Text);

                    Decoder decoder = Encoding.ASCII.GetDecoder();
                    byte[] bytesToConvert = (byte[])charBytes.ToArray(typeof(byte));

                    char[] chars = new char[decoder.GetCharCount(bytesToConvert, 0, bytesToConvert.Length)];
                    decoder.GetChars(bytesToConvert, 0, bytesToConvert.Length, chars, 0);

                    tokens.Add(new Token(new string(chars), bytes, startIndex, index - startIndex));
                }
                else
                {
                    ByteArrayEntry byteArrayEntry = new ByteArrayEntry(bytes[index], bytes[index+1]);
                    if( s_bitsToMnemonic.ContainsKey(byteArrayEntry) )
                    {
                        tokens.Add(new Token(s_bitsToMnemonic[new ByteArrayEntry(bytes[index], bytes[index + 1])], bytes, index, 2));
                    }
                    else
                    {
                        tokens.Add(new Token(bytes[index], bytes[index + 1]));
                    }
                    index += 2;
                }
            }
            return tokens.AsReadOnly();
        }
Example #3
0
 /// <summary>Creates a <see cref="Token"/> from two bytes.
 /// This is only to be used if the Daide Connection doesn't contain
 /// a predefined token for the bytes (in case the Daide protocol
 /// is updated without this framework being updated).
 /// </summary>
 /// <param name="high">The high byte.</param>
 /// <param name="low">The low byte.</param>
 /// <returns>A token that corresponds to the two bytes.</returns>
 public static Token FromBytes(byte high, byte low)
 {
     ByteArrayEntry bytes = new ByteArrayEntry(high, low);
     if( s_bitsToMnemonic.ContainsKey(bytes) )
     {
         return new Token(s_bitsToMnemonic[bytes], bytes.Bytes);
     }
     else
     {
         return new Token(bytes.Bytes);
     }
 }