ATLAS Offline Software
Loading...
Searching...
No Matches
binStrToHexStr.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef GLOBALSIM_BINSTRTOHEXSTR_H
6#define GLOBALSIM_BINSTRTOHEXSTR_H
7
8#include <algorithm>
9#include <ranges>
10#include <stdexcept>
11#include <string>
12#include <string_view>
13
14namespace GlobalSim {
15
16 constexpr char
17 hexChar(std::string_view bits){
18 unsigned int value{};
19 for (const char c : bits) {
20 value = (value << 1) | (c == '1');
21 }
22 // Upper case: the implementations these files are compared against -- GoldenGate
23 // and the firmware simulation -- both write upper case hex.
24 return (value < 10) ? static_cast<char>('0' + value) : static_cast<char>('A' + value - 10);
25 }
26
27 inline std::string
28 binStrToHexStr(std::string_view s){
29 if (s.starts_with("0b") || s.starts_with("0B")) {
30 s.remove_prefix(2);
31 }
32 if (s.empty()) {
33 return {};
34 }
35 if (!std::ranges::all_of(s, [](char c) { return c == '0' || c == '1';})) {
36 throw std::invalid_argument("binary string contains non-binary character");
37 }
38 std::string result;
39 result.reserve((s.size() + 3) / 4);
40 const auto firstGroupSize = s.size() % 4;
41 std::size_t pos{};
42 if (firstGroupSize != 0) {
43 result.push_back(hexChar(s.substr(0, firstGroupSize)));
44 pos = firstGroupSize;
45 }
46 for (; pos < s.size(); pos += 4) {
47 result.push_back(hexChar(s.substr(pos, 4)));
48 }
49 return result;
50 }
51
52} // namespace GlobalSim
53
54#endif
AlgTool to read in LArStripNeighborhoods, and run the BDT Algorithm.
Definition BitSpec.h:23
std::string binStrToHexStr(std::string_view s)
constexpr char hexChar(std::string_view bits)