How to print unsigned char[] as HEX in C++?

前端 未结 1 1394
温柔的废话
温柔的废话 2020-12-17 16:29

I would like to print the following hashed data. How should I do it?

unsigned char hashedChars[32];
SHA256((const unsigned char*)data.c_str(),
       data.le         


        
相关标签:
1条回答
  • 2020-12-17 17:22

    The hex format specifier is expecting a single integer value but you're providing instead an array of char. What you need to do is print out the char values individually as hex values.

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

    Since you are using C++ though you should consider using cout instead of printf (it's more idiomatic for C++.

    cout << "hashedChars: ";
    for (int i = 0; i < 32; i++) {
      cout << hex << hashedChars[i];
    }
    cout << endl;
    
    0 讨论(0)
提交回复
热议问题