Input(text): DEADBEEF Output(4 bytes array): DE AD BE EF How to do this?
+4
A:
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;
}
}
Thanks. It's work.
hlgl
2010-04-05 05:54:05
+2
A:
Sounds like you want to parse a string as hex into an integer. The C++ way:
#include <iostream>
#include <sstream>
#include <string>
template <typename IntType>
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<unsigned>(s);
std::cout << n << std::endl;
}
GMan
2010-04-04 18:47:56