2016-04-09 57 views
0

這是我的程序的主要(): - 只是試圖調試它。錯誤的複製值打印在c + +

int main() 
{ 
    cout<<"in main()"; 
    int i,x; 

    while(nr!=128 && nr!=192 && nr!=256) 
    { 
     //cout<<"Enter the lenghth of the key(128,192 or 256 only) : "; 
     //cin>>nr; 
    nr=128; 
    } 
    cout<<"after while when the value of nr is : "<<nr; 
    nk = nr/32; 
    nr = nk + 6; 
    cout<<endl<<"now the value of nr = "<<nr<<" and nk = "<<nk; 
    int ascii_table[26]={ 
      0x61, 
      0x62, 
      0x63, 
      0x64, 
      0x65, 
      0x66, 
      0x67, 
      0x68, 
      0x69, 
      0x6A, 
      0x6B, 
      0x6C, 
      0x6D, 
      0x6E, 
      0x6F, 
      0x70, 
      0x71, 
      0x72, 
      0x73, 
      0x74, 
      0x75, 
      0x76, 
      0x77, 
      0x78, 
      0x79, 
      0x7A 

     }; 
     string characters="abcdefghijklmnopqrstuvwxyz"; 
      // string bla="0x"; 
      cout<<"Enter plain text :"; 
      string str="disha"; 
      char msg[32]; 
      // string message[32]; 
      //cin>>str; 
      cout<<str; 
       for(int i=0;i<str.length();i++){ 
       int find=0; 
       while(1){ 
        if(str[i]==characters[find])break; 
        find++; 
       } 
        msg[i]=ascii_table[find]; 
      } 

      for(int i=0;i<str.length();i++) 
      {int h=msg[i]; 
       cout<<std::hex<<h<<"\t";} 


      char temp[32] = {0x00 ,0x01 ,0x02 ,0x03 ,0x04 ,0x05 ,0x06 ,0x07 ,0x08 ,0x09 ,0x0a ,0x0b ,0x0c ,0x0d ,0x0e ,0x0f}; 
      cout<<endl; 
      x=nk*4; 
      cout<<"before for loop nk = "<<nk<<" and nk*4 = "<<x; 
      /*for(i=0;i<x;i++) 
      { 
       cout<<" inside for loop "; 
       Key[i]=temp[i]; 
       in[i]=msg[i]; 
       cout<<in[i]<<'\t'; 
      } 
      (cout<<endl<<"after for loop"; 
       KeyExpansion(); 

    // The next function call encrypts the PlainText with the Key using AES algorithm. 
    Cipher(); 
    cout<<"\nText after encryption:\n"; 
    for(i=0;i<nb*4;i++) 
    { 
     cout<<out[i]; 
    } 
    cout<<endl;*/ 
    return 0; 
} 

它運行後,我得到意想不到的輸出 這是在main(),而之後的output--

當NR的值是:128 enter code here 現在的NR = 10的值,並NK = 4Enter純文本:disha64 69 73 68 61
之前 for循環NK = 4和NK * 4 = 10

當NK = 4,那麼x = nk個×4 = 16 但它打印10.如何? ??

+1

我認爲這是打印的十六進制值0x10是16個基點10 – Pemdas

+1

這段代碼格式非常糟糕。你能把它整理一下嗎?您還應該刪除不相關的註釋部分;我不明白爲什麼我們需要他們包括在問題中。 –

回答

1
cout<<std::hex<<h<<"\t"; 

這使得任何額外的輸出

+0

非常感謝..你是對的,它現在可以工作 –

1

值是在十六進制顯示,因爲你已經在你的代碼中使用cout<<std::hex以前默認的輸出格式hexidecmial。在此cout<<"before for loop nk = "<<nk<<" and nk*4 = "<<x;行之前添加cout<<std::dec以將輸出格式更改爲十進制。