mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-04-29 16:25:42 +00:00
adds binary to decimal and vice versa methods
This commit is contained in:
parent
b6a1acabf8
commit
b7b4ced8d6
1 changed files with 29 additions and 0 deletions
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue