CDigest CDigest::fromString(const std::string & s) { if (s.length() < 2*DIGEST_LENGTH) return CDigest(); CDigest r; for (auto i=0; i<DIGEST_LENGTH; ++i) { const char c0 = s[2*i+0]; const char c1 = s[2*i+1]; if (! (isxdigit(c0) && isxdigit(c1)) ) return CDigest(); r._data[i]= (charToByte(c0) << 4) | charToByte(c1); } return r; }
int main(){ struct binary_byte binary; int i = 0; binary = charToByte('Z'); printf("Testing %c is %d:\nIn binary: ", 'Z', 'Z'); for(i = 7; i >= 0; i--){ printf("%d ", binary.data[i]); } printf("\nDecoded: %c\n", byteToChar(binary)); }
bool xpcc::CanLawicelFormatter::convertToCanMessage(const char* in,can::Message& out) { uint8_t dlc_pos; if (in[0] == 'R' || in[0] == 'T') { out.flags.extended = true; dlc_pos = 9; } else { out.flags.extended = false; dlc_pos = 4; } if (std::strlen(in) < dlc_pos + 1U) return false; // get the number of data-bytes for this message out.length = in[dlc_pos] - '0'; if (out.length > 8) return false; // too many data-bytes if (in[0] == 'r' || in[0] == 'R') { out.flags.rtr = true; if (std::strlen(in) != (dlc_pos + 1U)) return false; } else { out.flags.rtr = false; if (std::strlen(in) != (out.length * 2 + dlc_pos + 1U)) return false; } // read the messge-identifier if (out.flags.extended) { uint16_t id; uint16_t id2; id = hexToByte(&in[1]) << 8; id |= hexToByte(&in[3]); id2 = hexToByte(&in[5]) << 8; id2 |= hexToByte(&in[7]); out.identifier = (uint32_t) id << 16 | id2; } else { uint16_t id; id = charToByte(&in[1]) << 8; id |= hexToByte(&in[2]); out.identifier = id; } // read data if the message is no rtr-frame if (!out.flags.rtr) { const char *buf = &in[dlc_pos + 1]; uint8_t i; for (i=0; i < out.length; i++) { out.data[i] = hexToByte(buf); buf += 2; } } return true; }