Quantcast
Channel: Форум программистов и сисадминов Киберфорум
Viewing all articles
Browse latest Browse all 517363

Перевести ASCII код в двоичный формат - C# Windows Forms

$
0
0
Я считываю с файла слова.Оно переводится в ASCII код. Не получается перевести ASCII код в двоичный формат. У меня должно получится что-то вроде такого : есть буква к примеру s ASCII код = 163 в двоичном виде 01110011 и нужно этот двоичный записать в массив ,то есть key[0] = 0 , key[1]=1 и т.д. Не понимаю как это сделать
вот код программы только этого пока не хватает
Код:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            FileInfo file = new FileInfo("vbbook.txt");
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            StreamReader streamReader = new StreamReader("vbbook.txt");
            string str = "";
            while (!streamReader.EndOfStream)
            {
                str += streamReader.ReadLine() + Environment.NewLine;
            }
            textBox1.Text = str;
            streamReader.Close();
        }
       
        private void button2_Click(object sender, EventArgs e)
        {
            string text = File.ReadAllText("vbbook.txt");
            byte[] ascii = Encoding.ASCII.GetBytes(text);
            string output = "";
            int k = 0;
            byte[] mass = new byte[text.Length];
            int j = 0;
            int i = 0;
            int[] key = new int[8];
            do
            {
                int key= ascii[i] % 2;
                output = tmp.ToString() + output;
                ascii[i] = ascii[i] / 2;
            }
            while (ascii[i] > 0);
            StreamWriter write_text;
            FileInfo file = new FileInfo("vbbook.txt");
            write_text = file.AppendText();
            write_text.WriteLine(ascii);
            write_text.Close();
           
           
        }

        public static string Encrypt(byte[] arr)
        {
            int[,] _Matrix = new int[8, 8] { { 0, 0, 0, 1, 0, 0, 1, 0 }, { 0, 0, 0, 0, 1, 0, 1, 0 }, { 1, 1, 1, 1, 1, 1, 1, 0 }, { 1, 0, 0, 1, 1, 0, 0, 1 }, { 1, 1, 0, 1, 0, 0, 1, 1 }, { 0, 1, 0, 0, 1, 0, 0, 1 }, { 1, 0, 1, 1, 0, 1, 0, 1 }, { 1, 0, 0, 1, 1, 1, 1, 1 } };

            string[] bytes = arr.Select(i => Convert.ToString(i, 2)).ToArray();
            byte[] mass = new byte[arr.Length];
            for (int i = 0; i < 8; i++)
                for(int j=0; j<8;j++)
            {
              // mass[i] = (_Matrix[0,i] * word[k] + _Matrix[1,i] * word[k] + _Matrix[2,i] * word[k] + _Matrix[3,i] * word[k] + _Matrix[4,i] * word[k] + _Matrix[5,i] * word[k] + _Matrix[6,i] * word[k] + _Matrix[7,i] * word[k]);
            }
            return Encoding.GetEncoding(1251).GetString(mass);
        }
       
         
       

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            FileInfo file = new FileInfo("vbbook.txt");
            if (file.Exists == true)
            {
                file.Delete();
            }
            else MessageBox.Show("Файла не существует!!");
        }

       

        private void button4_Click_1(object sender, EventArgs e)
        {
            Stream myStream;
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "Text(*.txt)|*.txt";
            if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if ((myStream = openFile.OpenFile()) != null)
                {
                    string strfilename = openFile.FileName;
                    string filetext = File.ReadAllText(strfilename);
                    textBox1.Text = filetext;
                }
            }
        } 
    }
}


Вложения
Тип файла: rar WindowsFormsApplication1 - копия.rar (66.2 Кб)

Viewing all articles
Browse latest Browse all 517363

Trending Articles