Skip to content

Instantly share code, notes, and snippets.

View rdhiggins's full-sized avatar

Rodger Higgins rdhiggins

View GitHub Profile
@rdhiggins
rdhiggins / hex_string a collection
Created November 18, 2022 16:00
Takes a collection of binary data and creates a hex string from it.
template <typename STD_COLLECTION>
std::string as_hex_string(STD_COLLECTION&& collection) {
std::stringstream ss;
for (unsigned char c: std::forward<STD_COLLECTION>(collection))
ss << std::setfill('0') << std::setw(2) << std::hex << static_cast<short>(c) << " ";
return ss.str();
}
@rdhiggins
rdhiggins / Read File as String.cpp
Created November 18, 2022 15:59
Open a file with ifstream and read it in in its entirety with ostrstream
string file_as_string(const string& path) {
auto ss = ostringstream{};
ifstream input_file(path);
if (!input_file.is_open()) {
cerr << "[ERROR] could not open file ‘" << path << "‘.n";
exit(EXIT_FAILURE);
}
ss << input_file.rdbuf();
return ss.str();
}
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}