Browse Source

adds binary to decimal and vice versa methods

AzaezelX 4 years ago
parent
commit
b7b4ced8d6
1 changed files with 29 additions and 0 deletions
  1. 29 0
      Engine/source/math/mConsoleFunctions.cpp

+ 29 - 0
Engine/source/math/mConsoleFunctions.cpp

@@ -424,3 +424,32 @@ DefineEngineFunction(mGetSignedAngleBetweenVectors, F32, (VectorF vecA, VectorF
 
 
    return MathUtils::getSignedAngleBetweenVectors(vecA, vecB, norm);
    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();
+}