14

以下のハッシュデータを印刷したいと思います。どうすればいいですか?

unsigned char hashedChars[32];
SHA256((const unsigned char*)data.c_str(),
       data.length(), 
       hashedChars);
printf("hashedChars: %X\n", hashedChars);  // doesn't seem to work??
4

2 に答える 2

17

16進形式の指定子は単一の整数値を期待していますが、代わりにの配列を提供していますchar。必要なのは、char値を16進値として個別に出力することです。

printf("hashedChars: ");
for (int i = 0; i < 32; i++) {
  printf("%x", hashedChars[i]);
}
printf("\n");

coutC ++を使用しているので、代わりに使用することを検討する必要がありますprintf(C++ではより慣用的です。

cout << "hashedChars: ";
for (int i = 0; i < 32; i++) {
  cout << hex << hashedChars[i];
}
cout << endl;
于 2012-05-04T15:11:06.650 に答える
3

C++では

#include <iostream>
#include <iomanip>

unsigned char buf0[] = {4, 85, 250, 206};
for (int i = 0;i < sizeof buf0 / sizeof buf0[0]; i++) {
    std::cout << std::setfill('0') 
              << std::setw(2) 
              << std::uppercase 
              << std::hex << (0xFF & buf0[i]) << " ";
}
于 2020-12-22T14:04:46.473 に答える