[SOLVED] How to print number in binary

I am currently trying to create a lookup table for all 12 bit fixed point decimal places. The end result should look something like this:

0000 00000000 = 0.0
0000 00000001 = 0.000244140625
0000 00000010 = 0.00048828125
0000 00000011 = 0.000732421875

I already created a script that console logs all of the correct decimal values in order:

(the hyphens are there so I can remove the useless zeros at the end of every number that has them)

The only problem is that I am unsure of how to have the required binary prefix. Is there any way to print a binary number in the way I need?

I figured out a way

var TwelveBitDecimal = pc.createScript('twelveBitDecimal');
TwelveBitDecimal.prototype.initialize = function() {
    this.numba = 0;
    this.line = 1;
    this.topline = -1;
    this.prefix = "0.000";
    this.fullline = "0000000";
    this.shortline = "000";
    for(i=0;i<4096;i++)
    {
        if (this.line > 410)
        {
            this.prefix = "0.";
        }
        else if (this.line > 41)
        {
            this.prefix = "0.0";
        }
        else if (this.line > 5)
        {
            this.prefix = "0.00";
        }

        if (((this.line - 1) % 256).toString(2) >= 10000000)
        {
            this.fullline = "";
        }
        else if (((this.line - 1) % 256).toString(2) >= 1000000)
        {
            this.fullline = "0";
        }
        else if (((this.line - 1) % 256).toString(2) >= 100000)
        {
            this.fullline = "00";
        }
        else if (((this.line - 1) % 256).toString(2) >= 10000)
        {
            this.fullline = "000";
        }
        else if (((this.line - 1) % 256).toString(2) >= 1000)
        {
            this.fullline = "0000";
        }
        else if (((this.line - 1) % 256).toString(2) >= 100)
        {
            this.fullline = "00000";
        }
        else if (((this.line - 1) % 256).toString(2) >= 10)
        {
            this.fullline = "000000";
        }
        else if (((this.line - 1) % 256).toString(2) >= 0)
        {
            this.fullline = "0000000";
        }

        if (((this.line - 1) % 256) === 0)
        {
            this.topline += 1;
        }

        if ((this.topline).toString(2) >= 1000)
        {
            this.shortline = "";
        }
        else if ((this.topline).toString(2) >= 100)
        {
            this.shortline = "0";
        }
        else if ((this.topline).toString(2) >= 10)
        {
            this.shortline = "00";
        }
        else if ((this.topline).toString(2) >= 0)
        {
            this.shortline = "000";
        }

        console.log(this.shortline + "" + (this.topline).toString(2) + " " + this.fullline + "" +((this.line - 1) % 256).toString(2) + " = " + this.prefix + this.numba + "-");
        this.numba += 244140625;
        this.line += 1;
    }
};

This should perfectly replicate exactly what I need

And this is the entire list in case anyone was curious:

Roll a Ball | Code Editor

Iā€™m curious what you need it for :grinning:

Nothing at the moment, I just wanted to have a list of all 12 bit decimal values because I am weird like that. I could also find every 16 bit decimal value if I need to.

Also, the text file containing all this data is 127 kb large, so if I do decide to store every 16 bit decimal value as well it would be well over 16 times larger, probably higher than 20, so around 2.5 mb text file just for a lookup table.

1 Like

Idk how to do it in playcanvas but could you put it in a SQL table if that is helpful?

I dont know what an sql table is.

I just looked it up and I dont really need it, this was just for my own amusement. Manual lookup is fine.

If someone else needs a fixed point table I could do that for them, but I dont think playcanvas can even use fixed point.

1 Like