2012-10-30 62 views
-5

我必须为学校制作一个D-FlipFlop程序。要输入D和电子钟,我想使用2个文本框。 (我还必须制作一个包含AND,NAND,OR,NOR,XOR端口的3个端口的程序,但这已经可行)。从文本框中提取数字

输入的D可以是:000001111100000111111100000011

输入为E可以是:000111000111000111000111000111

textbox1值必须去picturebox。因此,通过01,您可以画出一条线,并使触发器成为可视对象。 textbox2的值需要去picturebox2

+0

myTextbox.Text?我需要在你的问题中看到一些代码! – LightStriker

+4

听起来像你最好开始写一些代码......如果这是一个学校作业,如果你没有理解基本的编码,你会不会对你有任何好处。如果教师问你你怎么办来了你的答案/解决方案..?那么你会在头灯看起来像一只鹿..! – MethodMan

+3

您已经描述了一个项目,但没有提出问题,您是否在阅读文本框中的文本,在图片框中绘制文本,绘制线条,计算结果,绘制结果等方面遇到问题。哪一部分困扰你? – Casperah

回答

0

这是可能的。实际上,您需要将000001111100000111111100000011000111000111000111000111000111转换为string作为字节数组byte[]。要做到这一点,我们将使用Convert.ToByte(object value, IFormatProvider provider)

string input = "BINARY GOES HERE"; 
int numOfBytes = input.Length/8; //Get binary length and divide it by 8 
byte[] bytes = new byte[numOfBytes]; //Limit the array 
for (int i = 0; i < numOfBytes; ++i) 
{ 
    bytes[i] = Convert.ToByte(input.Substring(8 * i, 8), 2); //Get every EIGHT numbers and convert to a specific byte index. This is how binary is converted :) 
} 

这将宣布一个新的字符串input,然后将其编码为一个字节数组(byte[])。我们实际上需要这个byte[]作为Stream,然后将其插入我们的PictureBox。要做到这一点,我们将使用MemoryStream

MemoryStream FromBytes = new MemoryStream(bytes); //Create a new stream with a buffer (bytes) 

最后,我们可以简单地使用该流来创建我们Image文件和文件设置为我们PictureBox

pictureBox1.Image = Image.FromStream(FromBytes); //Set the image of pictureBox1 from our stream 

private Image Decode(string binary) 
{ 
    string input = binary; 
    int numOfBytes = input.Length/8; 
    byte[] bytes = new byte[numOfBytes]; 
    for (int i = 0; i < numOfBytes; ++i) 
    { 
     bytes[i] = Convert.ToByte(input.Substring(8 * i, 8), 2); 
    } 
    MemoryStream FromBinary = new MemoryStream(bytes); 
    return Image.FromStream(FromBinary); 
} 

通过使用这个,你可以随时打电话,例如Decode(000001111100000111111100000011),它会产生rt它为您的图像。然后,您可以使用此代码来设置PictureBox

pictureBox1.Image = Decode("000001111100000111111100000011"); 

重要通告的Image属性:您可能会收到ArgumentException was unhandled: Parameter is not valid.这可能发生因无效的二进制代码。

谢谢,
我希望对您有所帮助:)