$treeview $search $mathjax $extrastylesheet
otsdaq
v2_03_00
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #include "otsdaq-core/Macros/BinaryStringMacros.h" 00002 00003 using namespace ots; 00004 00005 //======================================================================================================================== 00006 // binaryToHexString 00007 // convert a data buffer of <len> bytes to a hex string 2*len characters long 00008 // Note: no preamble is applied by default (but "0x" could be nice) 00009 // 00010 // Note: this is used with defaults by VisualSupervisor 00011 std::string BinaryStringMacros::binaryToHexString(const char* binaryBuffer, 00012 unsigned int numberOfBytes, 00013 const std::string& resultPreamble, 00014 const std::string& resultDelimiter) 00015 { 00016 std::string dest; 00017 dest.reserve(numberOfBytes * 2); 00018 char hexstr[3]; 00019 00020 for(unsigned int i = 0; i < numberOfBytes; ++i) 00021 { 00022 sprintf(hexstr, "%02X", (unsigned char)binaryBuffer[i]); 00023 if(i) 00024 dest += resultDelimiter; 00025 dest += hexstr; 00026 } 00027 return resultPreamble + dest; 00028 } // end binaryToHexString 00029 00030 //======================================================================================================================== 00031 // binaryTo8ByteHexString 00032 // convert a data buffer string a hex string 00033 // 8 bytes at a time with the least significant byte last. 00034 // Note: no preamble is applied by default (but "0x" could be nice) 00035 std::string BinaryStringMacros::binaryTo8ByteHexString(const std::string& binaryBuffer, 00036 const std::string& resultPreamble, 00037 const std::string& resultDelimiter) 00038 { 00039 std::string dest; 00040 dest.reserve(binaryBuffer.size() * 2 + 00041 resultDelimiter.size() * (binaryBuffer.size() / 8) + 00042 resultPreamble.size()); 00043 char hexstr[3]; 00044 00045 dest += resultPreamble; 00046 00047 unsigned int j = 0; 00048 for(; j + 8 < binaryBuffer.size(); j += 8) 00049 { 00050 if(j) 00051 dest += resultDelimiter; 00052 for(unsigned int k = 0; k < 8; ++k) 00053 { 00054 sprintf(hexstr, "%02X", (unsigned char)binaryBuffer[7 - k + j * 8]); 00055 dest += hexstr; 00056 } 00057 } 00058 for(unsigned int k = binaryBuffer.size() - 1; k >= j; --k) 00059 { 00060 sprintf(hexstr, "%02X", (unsigned char)binaryBuffer[k]); 00061 dest += hexstr; 00062 if(k == 0) 00063 break; // to handle unsigned numbers when j is 0 00064 } 00065 00066 return dest; 00067 } // end binaryTo8ByteHexString