1 #include "otsdaq/Macros/BinaryStringMacros.h"
11 std::string BinaryStringMacros::binaryStringToHexString(
const void* binaryBuffer,
12 unsigned int numberOfBytes,
13 const std::string& resultPreamble,
14 const std::string& resultDelimiter)
17 dest.reserve(numberOfBytes * 2);
20 for(
unsigned int i = 0; i < numberOfBytes; ++i)
22 sprintf(hexstr,
"%02X", (
unsigned char)((
const char*)binaryBuffer)[i]);
24 dest += resultDelimiter;
27 return resultPreamble + dest;
34 std::string BinaryStringMacros::binaryNumberToHexString(
const std::string& binaryBuffer,
35 const std::string& resultPreamble ,
36 const std::string& resultDelimiter )
38 return binaryNumberToHexString(&binaryBuffer[0], binaryBuffer.size(), resultPreamble, resultDelimiter);
45 std::string BinaryStringMacros::binaryNumberToHexString(
const void* binaryBuffer,
46 unsigned int numberOfBytes,
47 const std::string& resultPreamble ,
48 const std::string& resultDelimiter )
51 dest.reserve(numberOfBytes * 2 + resultDelimiter.size() * (numberOfBytes / 8) + resultPreamble.size());
54 dest += resultPreamble;
57 for(; j + 8 < numberOfBytes; j += 8)
60 dest += resultDelimiter;
61 for(
unsigned int k = 0; k < 8; ++k)
63 sprintf(hexstr,
"%02X", (
unsigned char)((
const char*)binaryBuffer)[7 - k + j * 8]);
67 for(
unsigned int k = numberOfBytes - 1; k >= j; --k)
69 sprintf(hexstr,
"%02X", (
unsigned char)((
const char*)binaryBuffer)[k]);
81 void BinaryStringMacros::insertValueInBinaryString(std::string& binaryBuffer,
const std::string& value,
unsigned int bitIndex )
83 std::string dataType = StringMacros::getNumberType(value);
86 __SS__ <<
"String value must be a valid number! Value was " << value << __E__;
90 if(dataType ==
"double")
93 if(!StringMacros::getNumber<double>(value, v))
95 __SS__ <<
"String double value must be a valid number! Value was " << value << __E__;
98 BinaryStringMacros::insertValueInBinaryString<double>(binaryBuffer, v, bitIndex);
102 unsigned long long v;
103 if(!StringMacros::getNumber<unsigned long long>(value, v))
105 __SS__ <<
"String unsigned long long value must be a valid number! Value was " << value << __E__;
108 BinaryStringMacros::insertValueInBinaryString<unsigned long long>(binaryBuffer, v, bitIndex);
116 void BinaryStringMacros::extractValueFromBinaryString(
117 const void* binaryBufferVoid,
unsigned int bufferNumberOfBytes,
void* valueVoid,
unsigned int valueNumberOfBits,
unsigned int bitIndex )
123 if(valueNumberOfBits == 0)
125 __SS__ <<
"Can not extract value of size 0!" << __E__;
129 if(bitIndex + valueNumberOfBits > bufferNumberOfBytes * 8)
131 __SS__ <<
"Can not extract value of size " << valueNumberOfBits <<
", at position " << bitIndex <<
", from buffer of size " << bufferNumberOfBytes * 8
136 unsigned char* value = (
unsigned char*)valueVoid;
137 const unsigned char* binaryBuffer = (
const unsigned char*)binaryBufferVoid;
138 unsigned int byteOffset = bitIndex / 8;
139 unsigned int bitOffset = bitIndex % 8;
140 unsigned int bitsLeft = valueNumberOfBits;
144 unsigned int valuei = 0;
155 value[valuei] = binaryBuffer[byteOffset] >> bitOffset;
157 value[valuei] |= binaryBuffer[byteOffset + 1] << (8 - bitOffset);
167 unsigned char keepMask = ((
unsigned char)(-1)) >> (8 - bitsLeft);
168 value[valuei] &= keepMask;
187 void BinaryStringMacros::extractValueFromBinaryString(
const std::string& binaryBuffer,
189 unsigned int valueNumberOfBits,
190 unsigned int bitIndex )
192 value.resize((valueNumberOfBits / 8) + ((valueNumberOfBits % 8) ? 1 : 0));
194 __COUTV__(value.size());
196 extractValueFromBinaryString(&binaryBuffer[0], binaryBuffer.size(), &value[0], valueNumberOfBits, bitIndex);