Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / Solutions Sem. 03 / ex4 / Bytex.cpp
diff options
context:
space:
mode:
1 files changed, 46 insertions, 0 deletions
-rw-r--r--Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/Bytex.cpp46
diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/Bytex.cpp b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/Bytex.cpp
new file mode 100644
index 0000000..42a03c6
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/Bytex.cpp
@@ -0,0 +1,46 @@
+#include "Bytex.h"
+
+char Bytex::digitToHexChar(unsigned int digit) {
+ if (digit < 10)
+ return '0' + digit;
+ return 'A' + digit - 10;
+}
+
+int Bytex::hexCharToNum(char hexChar) {
+ if (hexChar <= '9')
+ return hexChar - '0';
+ return hexChar - 'A' + 10;
+}
+
+void Bytex::byteValToHex() {
+ int front = byteVal / 16, back = byteVal % 16;
+ hexVal[0] = digitToHexChar(front);
+ hexVal[1] = digitToHexChar(back);
+}
+
+void Bytex::hexValToByte() {
+ byteVal = hexCharToNum(hexVal[0]) * 16 + hexCharToNum(hexVal[1]);
+}
+
+Bytex::Bytex() : Bytex('\0')
+{}
+
+Bytex::Bytex(char byteVal) {
+ this->byteVal = byteVal;
+ byteValToHex();
+}
+
+Bytex::Bytex(const char* hexVal) {
+ this->hexVal[0] = hexVal[0];
+ this->hexVal[1] = hexVal[1];
+ hexValToByte();
+}
+
+char Bytex::getByte() const {
+ return byteVal;
+}
+
+void Bytex::getHex(char* output) const {
+ output[0] = hexVal[0];
+ output[1] = hexVal[1];
+}