adds binary to decimal and vice versa methods

This commit is contained in:
AzaezelX 2021-04-02 14:08:26 -05:00
parent b6a1acabf8
commit b7b4ced8d6

View file

@ -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();
}