Esempio n. 1
0
        void UpdateEncoded()
        {
            List <byte> bytes = new List <byte>();

            for (long i = selByte1; i < selByte2; i++)
            {
                bytes.Add(file[i]);
            }


            string encoded = "Encode failed";

            // toarray exists but we need a workaround because of obscure practices by bitconverter class
            byte[] bytesArr = new byte[bytes.Count];
            bytesArr = bytes.ToArray();

            if (rb_Integer.Checked)
            {
                if (bytesArr.Length > 8)
                {
                    encoded = "Too big section\r\nTry a smaller range";
                }
                else if (bytesArr.Length < 8)
                {
                    encoded = "Too small section\r\nTry a bigger range";
                }
                else
                {
                    // lol this is
                    try
                    {
                        encoded = BitConverter.ToUInt64(bytesArr, 0).ToString();
                    }
                    catch
                    {
                        try
                        {
                            encoded = BitConverter.ToInt64(bytesArr, 0).ToString();
                        }
                        catch { }
                    }
                }
            }
            else if (rb_HexStr.Checked)
            {
                encoded = ExtensionMethods.ByteArrayToString(bytesArr);
            }
            else if (rb_UTF8.Checked)
            {
                if (ignoreNUL)
                {
                    for (int i = 0; i < bytesArr.Length; i++)
                    {
                        if (bytesArr[i] == 0)
                        {
                            bytesArr[i] = (byte)'0';
                        }
                    }
                }

                // we run into a dilemma
                // how to ignore nul terminator?
                encoded = System.Text.Encoding.UTF8.GetString(bytesArr);
            }
            else if (rb_ASCII.Checked)
            {
                if (ignoreNUL)
                {
                    for (int i = 0; i < bytesArr.Length; i++)
                    {
                        if (bytesArr[i] == 0)
                        {
                            bytesArr[i] = (byte)'0';
                        }
                    }
                }

                encoded = System.Text.Encoding.ASCII.GetString(bytesArr);
            }
            txt_Encoded.Text = encoded;
        }