summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--number.cpp48
-rw-r--r--number.h10
2 files changed, 58 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
diff --git a/number.h b/number.h
index ca5075e..e7eebb5 100644
--- a/number.h
+++ b/number.h
@@ -9,6 +9,16 @@ public:
number() = default;
/**
+ * \brief Return a hexadecimal string representation of this number.
+ */
+ std::string to_hex_string() const;
+
+ /**
+ * \brief Return a decimal string representation of this number.
+ */
+ std::string to_dec_string() const;
+
+ /**
* \brief Return the result of the addition of a number and this number.
*
* \param n The number to add to this number.