The question comprises 2 steps: parse XML and convert floats to their representation in IEEE 754 as Hex value.
For parsing XML, there are several libraries out there you can simply reuse. You only have to consider if your input will always be a complete, valid XML, or only fragments that cannot stand by themselves. I am not sure it this is the case in your question.
For converting floats to IEEE 754 hex, most systems internally represent floats in IEEE 754. If this is the case for your system, you just have to find get to the representation.
You can use this (with input from this question):
std::string floatToIEEE754(float f) {
if (!std::numeric_limits<float>::is_iec559) {
std::cerr << "Not in IEEE 754 format!" << std::endl;
return "";
}
std::stringstream ss;
ss << std::hex << std::setw(2);
ss << static_cast<int> (reinterpret_cast<unsigned char*>(&f)[0]);
ss << static_cast<int> (reinterpret_cast<unsigned char*>(&f)[1]);
ss << static_cast<int> (reinterpret_cast<unsigned char*>(&f)[2]);
ss << static_cast<int> (reinterpret_cast<unsigned char*>(&f)[3]);
// Or to get it as int:
//int i;
//ss >> i;
//return i;
return ss.str();
}
The weird casts are necessary to ensure compliance with the language. Strictly, C++ has undefined behaviour if you cast one pointer type to another (as in int i = *((int*)&f);
). Only casts to char*
are guaranteed to work.
If you have no strict requirements for the system, you can simplify the method as follows:
std::string floatToIEEE754(float f) {
std::stringstream ss;
// Treat bits of float as an int, and print them as hex
ss << std::hex << *((int*) &f);
return ss.str();
}