2012-10-30 63 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.這可能發生因無效的二進制代碼。

謝謝,
我希望對您有所幫助:)