我有一個字符串,代表base64中的值。我想將此字符串從base64轉換爲十六進制。我在Ubuntu 10.10上使用C++。我有以下代碼:如何將base64字符串轉換爲C++中的十六進制字符串?
std::string ssir = "DNQwSinfOUSSWd+U04r23A==";
std::string dec=(base64_decode(ssir));
std::stringstream ss;
for (int i=0; i<dec.size(); ++i) {
if (i != 0) ss << ':';
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(dec[i]);//(int)(dec[i]);
}
std::string res;
res= ss.str();
cout<<"the ress is: "<<res<<std::endl;
結果是:
0C:ffffffd4:30:4A:29:ffffffdf:39:44:ffffff92:59:ffffffdf:ffffff94:ffffffd3:ffffff8a :fffffff6:ffffffdc
這是正確的,除了那些ffffffff。我能做些什麼來解決這個問題?如果我想將我的十六進制結果寫入std::vector<unsigned char> x
,我該怎麼做?
請注意,你稱之爲「hexa結果」是一個字符串,因此將它存儲在`std :: string`中會更有意義。 – ereOn 2011-01-14 08:17:58
重複(昨天是同一個用戶):[base 64 string to hexa string](http://stackoverflow.com/questions/4680441/base-64-string-to-hexa-string) – 2011-01-14 08:32:26