# Decimal to binary converter

Hi guys,
Very simple question.
I want to do a game where you translate a decimal number to a binary one. Like you won points when you convert it well and u loose when u make errors.in the blueprint section, there is no section on binary to decimal converter. So how would I do it with bp or code?

In plain old C++

``````
float f = 1234.567890;
unsigned char* bytes = (unsigned char *) &f;
cout << (int) bytes[3] << " " << (int) bytes[2] << " "  << (int) bytes[1] << " "  << (int) bytes[0] << endl;

``````

You can get the individual bits from those bytes as well. The same works in reverse

``````
unsigned char bytes[4] = {44, 82, 154, 68};
float *f = (float *) bytes;
cout << *f << endl;

``````

You can check invidual bits this expression:

``````
n & FMath::Pow(2,x)

``````

n being a number and x being bit you check (not sure about 0 position, which should be first bit will work with pow… but there way to avoid it). How does it work? & is a AND gate operator, all power of 2 numbers and number 1 are 1 in invidual bit position in binery form ( 1=1, 2=10, 4=100, 8=1000) so if you do AND gate operation on series of bits againts 1 bit on specific position, it will return 0 if that invidual bit is 0. So in UE you can do this this way:

``````

FString FMyFanctMath::BitsToString(int32 n){

FString Bits;
Bits.Reserve(FMath::FloorToInt(FMath::Sqrt(n))); //Reserveing space for string in memory FString won't rellocate string multiple times in process
//We multiply by 2 until it's bigger then n where every bit will be 0 after that
for(i = 1;i<n;i*2) {
if(n & i) Bits.Append("1");
else Bits.Append("0");
}
return Bits.Reverse() //We need to reverse bit order, so smallest is last
}

``````

Obviuesly you can only present binery number in string, since any number is a number and can be presented in any form, UE4 presents number only in decimals, or else there also function for hex

C++14 allows to use binery form numbers in code, using “0b” prefix (same as “0x” for hex), but UE4 uses C++11 compiler mode if i’m not mistaken, so it won’t work in UE4

1 Like

Hi !! thx for fast reply! can u give me a personal email or a facebook so that i can give you more specificity…i promise it wont take that long!!!

ok so i guess its impossible to do that in blueprint right??thats what i thought…they should put it in the blueprint tho…cryengine 3 has the same problem!

No, it is possible in Blueprint i think, there is Bitwise AND node

Just do similar loop i showed you multiplaying i by 2 on each starting from 1, if result of AND is higher then 0 it means it has bit if 0 then no remember you don’t need special loop node for that you can loop nodes together too, you just need to provide exit ot else you get infinite loop

thx man! ill read the doc!

Ok i just read it man. I might have read it too much fast but I think that this is what I need. I dont see the need of what you said by multiplying by two and using bolean operator…
what If i just use Random integer BP (to generate a random number) and I connect it to ‘‘tobyte(int)’’ and then connect to the UI blueprint to it appears on my screen? wouldnt it work?

That will convert it to byte, not bits. 1 byte = 8 bits = 2 hex digits. Byte type is uint8 and it can hold unsigned number in max 1 byte (8 bits) so max number it can have is 255 and can’t go below 0 as it is unsigned. Intiger is int32 which can hold signed number in 4 bytes (32 bits) which means it can hold max number of 2147483647 in both - and + direction as it is signed. Those are only 2 integer types that reflection system (and as result blueprint), in C++ you got 8, 16, 32 and 64 (on 64-bit CPUs only) sign and unsigned as well TBigInt which you can hold infinity (as memory size allows) big int, but blueprint don’t support it

I know but I want my number range to be between 0 and 255 so one byte…so i think that this bp is just fine

another question: how do i do so that one touch of the keyboard or (joystick) equals 0 and another 1?

Same as any other input

can you answer me as if i was a noob please. i just have 3 months in the engine

try this one…

String str = “100”;
double j=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)== ‘1’){
j=j+ Math.pow(2,str.length()-1-i);
}
}

check…[)

If you need leading zeroes, like I did for sorting, you can use this (based on Shadowriver’s answer)

``````FString IntToBin(int32 n)
{
FString bits;
bits.Reserve(32);
for (int32 i = 0; i < 32; i++)
{
bits.Append(n & (1 << i) ? "1" : "0");
}
return bits.Reverse();
}
``````