From 53876ac54e6fe3293b4cf0bbb4f037960efb0126 Mon Sep 17 00:00:00 2001 From: Kevin Pearson Date: Thu, 27 Apr 2017 11:56:57 -0400 Subject: [PATCH] Add utility to convert string to uint16_t Purpose for this is for command argument parsing of vendor and product IDs Signed-off-by: Kevin Pearson --- src/helpers/utils.cpp | 8 ++++++++ src/helpers/utils.h | 1 + 2 files changed, 9 insertions(+) diff --git a/src/helpers/utils.cpp b/src/helpers/utils.cpp index 56c793d..0ea5e0d 100644 --- a/src/helpers/utils.cpp +++ b/src/helpers/utils.cpp @@ -216,4 +216,12 @@ namespace utils { return true; } + bool parseUInt16(std::string val, uint16_t &uint16) { + if (val.length() == 1) val = "0" + val; + if (val.length() == 2) val = "0" + val; + if (val.length() == 3) val = "0" + val; + if (val.length() != 4) return false; + uint16 = std::stoul("0x" + val, nullptr, 16); + return true; + } } diff --git a/src/helpers/utils.h b/src/helpers/utils.h index 5b7a52c..92de26b 100644 --- a/src/helpers/utils.h +++ b/src/helpers/utils.h @@ -16,6 +16,7 @@ namespace utils { bool parseColor(std::string val, LedKeyboard::Color &color); bool parseSpeed(std::string val, uint8_t &speed); bool parseUInt8(std::string val, uint8_t &uint8); + bool parseUInt16(std::string val, uint16_t &uint16); }