From b7b4ced8d6f14632ceb2012dabb8590ce7782fd2 Mon Sep 17 00:00:00 2001 From: AzaezelX Date: Fri, 2 Apr 2021 14:08:26 -0500 Subject: [PATCH] adds binary to decimal and vice versa methods --- Engine/source/math/mConsoleFunctions.cpp | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Engine/source/math/mConsoleFunctions.cpp b/Engine/source/math/mConsoleFunctions.cpp index 5252d2306..0113e9126 100644 --- a/Engine/source/math/mConsoleFunctions.cpp +++ b/Engine/source/math/mConsoleFunctions.cpp @@ -424,3 +424,32 @@ DefineEngineFunction(mGetSignedAngleBetweenVectors, F32, (VectorF vecA, VectorF return MathUtils::getSignedAngleBetweenVectors(vecA, vecB, norm); } + +DefineEngineFunction(mBinToDec, S32, (String n),,"convert a binary to decimal") +{ + String num = n; + int dec_value = 0; + + // Initializing base value to 1, i.e 2^0 + int base = 1; + + int len = num.length(); + for (int i = len - 1; i >= 0; i--) { + if (num[i] == '1')//pick out our 1s and concatenate + dec_value += base; + base = base * 2;//next power of 2 + } + + return dec_value; +} + +DefineEngineFunction(mDecToBin, const char*, (S32 n), , "convert decimal to a binary") +{ + String ret; + while (n > 0) { + int r = n % 2;//modulus aka remainder of 2. nets you a 0 or a 1 + n /= 2;//next power of 2 + ret = String::ToString("%i",r) + ret;//add to the front of the stack + } + return ret.c_str(); +}