- //DumpMessage.cpp
- #include "../cryptlib/cryptlib.h"
- #include "../cryptlib/filters.h"
- #include <iostream>
- void DumpMessage(CryptoPP::BufferedTransformation& bt){
- using namespace std;
- static char const* szHexDigits = "0123456789abcdef";
- byte b;
- while(bt.AnyRetrievable()){
- if(!bt.Get(b))
- throw "Error: AnyRetrievable() returned true,""so this shouldn't happen";
- //it is much easier to implement this using HexEncoder;
- //however, let's not get into that just yet. this below code
- //could do some special kind of processing that is not
- //supported by off-the-shelf Filter class.
- cout << szHexDigits[(b >> 4) & 0x0f]
- << szHexDigits[b & 0x0f]
- << ' ';
- }
- }
- int main(){
- using namespace CryptoPP;
- char const* szMessage = "How do you do?";
- StringSource ss(szMessage, true);
- DumpMessage(ss);
- //if we constructed stringsource with 'false' instead of true,
- // stringsource wouldn't call its PumpAll() method on construction,
- //and no data would be extractable from the stringsource object
- //until someone called the object's Pump() or PumpAll() method
- return 0;
- }
userguide中的原代码无法编译通过,会报一个invalid initialization of non-const reference of type ... from a temporary of type的错误。
所以将其修改为30行的代码,将StringSource对象用构造函数构造成一个具有生命的变量(am I right),就可以解决问题了。