Skip to content

Instantly share code, notes, and snippets.

@regen100
Created December 10, 2016 14:07
Show Gist options
  • Save regen100/dfe6fa52fa98d9bd2709c3392145e267 to your computer and use it in GitHub Desktop.
Save regen100/dfe6fa52fa98d9bd2709c3392145e267 to your computer and use it in GitHub Desktop.
Get Raspberry Pi serial number
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdint>
#include <iomanip>
#include <experimental/optional>
/*
* Get Raspberry Pi serial number
*/
std::experimental::optional<std::uint32_t> get_serial() {
std::ifstream finfo("/proc/cpuinfo");
std::string line;
while (std::getline(finfo, line)) {
if (line.substr(0, 6) == "Serial") {
std::stringstream ss;
ss << std::hex << line.substr(10, 26);
std::uint32_t serial;
ss >> serial;
if (!ss.fail()) {
return serial;
}
}
}
return {};
}
int main() {
auto serial = get_serial();
if (serial) {
std::cout << std::hex << std::setw(16) << std::setfill('0') << *serial << std::endl;
} else {
std::cout << "fail to get serial" << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment