map<string, CanVariable> CanIdTranslator::translateData(vector<XmlNode> variableNodes, string dataHex) { map<string, CanVariable> variables; string dataBin = hex2bin(dataHex); //dataBin = rpad(dataBin, 64, '0'); for (int c = 0; c < variableNodes.size(); c++) { map<string, string> attributes = variableNodes[c].getAttributes(); int bitStart = stoi(attributes["start_bit"]); int bitLength = stoi(attributes["bit_length"]); string bits; try { bits = dataBin.substr(bitStart, bitLength); } catch (std::out_of_range& e) { return variables; } CanVariable variable; variable.setName(attributes["name"]); variable.setType(attributes["type"]); variable.setUnit(attributes["unit"]); variable.setStartBit(bitStart); variable.setBitLength(bitLength); if (attributes["type"] == "uint") { variable.setValue(utos(bin2uint(bits))); } else if (attributes["type"] == "float") { variable.setValue(ftos(bin2float(bits))); } else if (attributes["type"] == "enum") { vector<XmlNode> values = variableNodes[c].getChildren(); string value = utos(bin2uint(bits)); for (int k = 0; k < values.size(); k++) { map<string, string> valueAttributes = values[k].getAttributes(); variable.addEnumValue(valueAttributes["id"], valueAttributes["name"]); if (valueAttributes["id"] == value) { variable.setValue(valueAttributes["name"]); } } } else if (attributes["type"] == "ascii") { string str; for (int k = 0; k < bits.length(); k += 8) { try { str += (char)bin2uint(bits.substr(k, 8)); } catch (std::out_of_range& e) { cout << "DEBUG: TranslateData exception: " << e.what() << "\n"; cout << "DEBUG: B bits=" << bits << " :: k=" << k << endl; continue; } } variable.setValue(str); } else if (attributes["type"] == "hexstring") { string str; for (int k = 0; k < bits.length(); k += 4) { try { str += bin2hex(bits.substr(k, 4)); } catch (std::out_of_range& e) { cout << "DEBUG: TranslateData exception: " << e.what() << "\n"; cout << "DEBUG: C bits=" << bits << " :: k=" << k << endl; continue; } } variable.setValue(str); } variables[attributes["name"]] = variable; } return variables; }
int main(){ char array[] = "11000001010001011100001010001111"; printf("%f\n",bin2float(array)); }