c++ - Converting an XML stream to hexadecimal -


so rather new c++, , hope contructive advice.

i working on telemetry system scientific rocket takes data instrument, stores data pipe software, , subsequently sends data available serialport. problem instrument transmits data packets in xml format, e.g.:

<sample value="-4.80521e-012" /> <sample value="4.90272e-012" /> <sample value="3.49013e-011" /> <sample value="2.13785e-010" /> <sample value="2.38185e-010" /> <sample value="1.70573e-010" /> <sample value="1.16129e-011" /> 

these stored temporarily in buffer created createfile/writefile (from serial port). probe cannot send other formats xml, , need convert implicitly or explicitly 4 byte hex (due telemetry requirements), e.g.:

2c34b73f 2c1dfc77 2bbd69d2 a9220b89 a8a0cedf 290bc781... 

my question then: can suggest way this? should try remove sets of substrings stream, or easier way translate between xml , hex? note again done in c++.

best regards,

tarjei

the question comprises 2 steps: parse xml , convert floats representation in ieee 754 hex value.

for parsing xml, there several libraries out there can reuse. have consider if input complete, valid xml, or fragments cannot stand themselves. not sure case in question.

for converting floats ieee 754 hex, systems internally represent floats in ieee 754. if case system, have find representation.

you can use (with input 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 int:     //int i;     //ss >> i;     //return i;      return ss.str(); } 

the weird casts necessary ensure compliance language. strictly, c++ has undefined behaviour if cast 1 pointer type (as in int = *((int*)&f);). casts char* guaranteed work.

if have no strict requirements system, can simplify method follows:

std::string floattoieee754(float f) {     std::stringstream ss;     // treat bits of float int, , print them hex     ss << std::hex << *((int*) &f);     return ss.str(); } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -