Created
December 10, 2016 14:07
-
-
Save regen100/dfe6fa52fa98d9bd2709c3392145e267 to your computer and use it in GitHub Desktop.
Get Raspberry Pi serial number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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