這是一個我認爲很簡單的問題。我搞亂了Crypto ++ lib和我在SO上找到的sample。所以我試圖在「轉儲密碼文本」部分輸出文本/ ASCII字符(而不是十六進制)。用C++程序在不同基地輸出不可打印字符?
這部分(原件):
//
// Dump Cipher Text
//
std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;
for(int i = 0; i < ciphertext.size(); i++) {
std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << " ";
}
std::cout << std::endl << std::endl;
而且我發現一個奇怪的現象......在ciphertext.size()
返回不同的字節數後,我試着這樣做:
我編輯的部分:
//
// Dump Cipher Text
//
std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl; // returns 16 bytes.
int num = ciphertext.size(); // num returns 16 here
for (int i = 0; i < ciphertext.size(); i++) {
std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << " ";
}
std::cout << std::endl << std::endl;
std::cout << "Ciphertext size: " << ciphertext.size() << std::endl; // now it's 10 bytes?
std::cout << "num: " << num; // returns 10 bytes?! what the hell...
std::cout << std::endl << std::endl;
所以我碰上了這一點,因爲我試圖打印的ASCII字符,而不是十六進制字節,它的工作,但我只是不明白爲什麼...
正因爲如此:
std::cout << "To Text: ";
for (int x = 0; x < ciphertext.size(); x++)
{
std::cout << ciphertext[x];
}
打印所有ASCII字符(而不是十六進制),但...因爲ciphertext.size()
回報10,它不應該,因爲它是第一個被定義爲16打印所有字符(而不是10),但是,它確實打印完全一切......我真的很困惑。如果我將變量放置/複製到int
以確保它不會被更改,如何重新定義變量?
值相同,輸出的基數不同。 '0x10 hex'是'16' :)。 – AlexD