我有这个:
uint8_t key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
如何将其转换为char或其他东西以便我可以阅读其内容?这是我用来使用AES加密数据的密钥。
帮助表示赞赏。 感谢
答案 0 :(得分:4)
String converter(uint_8 *str){
return String((char *)str);
}
答案 1 :(得分:1)
如果我理解正确,您需要以下内容
#include <iostream>
#include <string>
#include <numeric>
#include <iterator>
#include <cstdint>
int main()
{
std::uint8_t key[] =
{
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
};
std::string s;
s.reserve( 100 );
for ( int value : key ) s += std::to_string( value ) + ' ';
std::cout << s << std::endl;
}
程序输出
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
如果您不需要,可以删除空白。
拥有字符串,您可以随意处理它。
答案 2 :(得分:1)
#include <sstream> // std::ostringstream
#include <algorithm> // std::copy
#include <iterator> // std::ostream_iterator
#include <iostream> // std::cout
int main(){
uint8_t key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
std::ostringstream ss;
std::copy(key, key+sizeof(key), std::ostream_iterator<int>(ss, ","));
std::cout << ss.str();
return 0;
}
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 ,24,25,26,27,28,29, 30,31,
答案 3 :(得分:0)
如果目标是构造一个包含2个字符的十六进制值的字符串,则可以使用带有IO操纵器的字符串流,如下所示:
std::string to_hex( uint8_t data[32] )
{
std::ostringstream oss;
oss << std::hex << std::setfill('0');
for( uint8_t val : data )
{
oss << std::setw(2) << (unsigned int)val;
}
return oss.str();
}
这需要标题:
<string>
<sstream>
<iomanip>