字符串到字节数组

如何输入DEADBEEF并将DE AD BE EF输出为四字节数组?

 void hexconvert( char *text, unsigned char bytes[] ) { int i; int temp; for( i = 0; i < 4; ++i ) { sscanf( text + 2 * i, "%2x", &temp ); bytes[i] = temp; } } 

听起来你想将字符串解析为hex整数。 C ++方式:

 #include  #include  #include  template  IntType hex_to_integer(const std::string& pStr) { std::stringstream ss(pStr); IntType i; ss >> std::hex >> i; return i; } int main(void) { std::string s = "DEADBEEF"; unsigned n = hex_to_integer(s); std::cout << n << std::endl; }