[英]Convert hexadecimal charArray to String
I have a serious and irritating problem, please help 我有一个严重的问题,请帮忙
mdContext->digest[i]
is an unsigned char Array with hexadecimal values so mdContext->digest[i]
是具有十六进制值的无符号char数组,因此
for (i = 0; i < 16; i++)
printf ("%02x", mdContext->digest[i]);
prints 900150983cd24fb0d6963f7d28e17f72
打印900150983cd24fb0d6963f7d28e17f72
now.... I want to get this value in a char Array, ie if I do 现在...。我想在char数组中获取此值,即如果我这样做
printf("%s",ArrayConverted);
I want to print the above string... Please help me in doing this 我想打印上面的字符串...请帮助我
Things I tried 我尝试过的事情
unsigned char in[64]=0;
int tempValue[64];
for (i = 0; i < 16; i++){
sprintf(&tempValue[i],"%02x", (unsigned char)mdContext->digest[i]);
in[i]=(unsigned char)tempValue[i];
}
printf("%s\n\n\n",in);
This prints 90593d4bd9372e77
But Original content is 900150983cd24fb0d6963f7d28e17f72
此打印90593d4bd9372e77
但原始内容为900150983cd24fb0d6963f7d28e17f72
So it is skipping many characters in between... please help me converting this hexadecimal Char array in to a String 所以它之间跳过了许多字符...请帮助我将十六进制Char数组转换为String
char tempValue[33]; // 32 hex digits + 0-terminator
int i;
for (i = 0; i < 16; ++i)
sprintf(tempValue + 2*i, "%02x", (unsigned char)mdContext->digest[i]);
Each byte requires two hexadecimal digits - so adjust the start position for sprintf
with 2*i
每个字节需要两个十六进制数字-因此将sprintf
的起始位置调整为2*i
tempValue + 2*i
is the same as &tempValue[2*i]
tempValue + 2*i
与&tempValue[2*i]
EDIT: A correct c++ version. 编辑:正确的c ++版本。
std::stringstream s;
for (int i = 0; i < 16; ++i)
s << std::hex << std::setfill('0') << std::setw(2) << (unsigned short) mdContext->digest[i];
std::cout << s.str() << std::endl;
C++ specific solution: C ++特定解决方案:
#include <sstream>
#include <iomanip>
std::stringstream s;
s.fill('0');
for ( size_t i = 0 ; i < 16 ; ++i )
s << std::setw(2) << std::hex <<(unsigned short)mdContext->digest[i]);
std::cout << s.str() << endl;
Small demo : http://ideone.com/sTiEn 小型演示: http : //ideone.com/sTiEn
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.