如何在C ++中将unsigned char []打印为HEX? [英] How to print unsigned char[] as HEX in C++?

查看:1498
本文介绍了如何在C ++中将unsigned char []打印为HEX?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印以下散列数据。我应该怎么办?

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.length(), 
       hashedChars);
printf("hashedChars: %X\n", hashedChars);  // doesn't seem to work??


推荐答案

十六进制格式说明符需要一个整数值,你提供了一个 char 的数组。您需要做的是单独打印作为十六进制值的 char 值。

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");

由于你使用的是C ++,但你应该考虑使用 cout 而不是 printf (它更适用于C ++。

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;

这篇关于如何在C ++中将unsigned char []打印为HEX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆