summaryrefslogtreecommitdiff
path: root/number.cpp
diff options
context:
space:
mode:
authorOlivier Gayot <og@satcom1.com>2017-11-18 13:42:45 +0100
committerOlivier Gayot <og@satcom1.com>2017-11-19 14:01:08 +0100
commitacab244a6342be7597d0f2d22dc90a43f425fbb8 (patch)
treefc9288bbea13abb702820a524cbc4ce6fb247ce5 /number.cpp
parent589f8e2558d008c63892f2345b233e28d889620b (diff)
Added functions to print a number as hex or dec string
Signed-off-by: Olivier Gayot <og@satcom1.com>
Diffstat (limited to 'number.cpp')
-rw-r--r--number.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/number.cpp b/number.cpp
index f6f445c..26b0ed2 100644
--- a/number.cpp
+++ b/number.cpp
@@ -1,5 +1,53 @@
+#include <iomanip>
+#include <sstream>
#include "number.h"
+/* Type Conversion {{{ */
+
+std::string
+number::to_hex_string() const
+{
+ if (_operands.empty()) {
+ return std::string("0x0");
+ }
+
+ std::ostringstream ss;
+
+ ss << "0x" << std::hex;
+
+ for (auto it = _operands.crbegin(); it != _operands.crend(); ++it) {
+ ss << *it;
+
+ /* If more operands are present, successive ones must be padded. */
+ ss << std::setfill('0');
+ ss << std::setw(16);
+ }
+
+ return ss.str();
+}
+
+std::string
+number::to_dec_string() const
+{
+ if (_operands.empty()) {
+ return std::string("0");
+ }
+
+ std::ostringstream ss;
+
+ ss << std::dec;
+
+ if (_operands.size() == 1) {
+ ss << _operands.front();
+ } else {
+ /* TODO */
+ ss << "[Not implemented]";
+ }
+
+ return ss.str();
+}
+
+/* }}} */
/* Operations {{{ */
number