Search

Introducing my online byte converter!

I’ve been meaning to try my hand at some real-world JavaScript, honestly one of the many reasons I wanted to start my own site. Now, get ready, ’cause this converter’s about to get heavy!

 function ByteConverter(valNum) {
            var selected = document.activeElement;
            if (selected && selected.id == "inputBytes") {
                document.getElementById("inputKilobytes").value = valNum / 1024;
                document.getElementById("inputMegabytes").value = valNum / 1048576;
                document.getElementById("inputGigabytes").value = valNum / 1073741824;
                document.getElementById("inputTerabytes").value = valNum / 1099511627776;
            }
            if (selected && selected.id == "inputKilobytes") {
                document.getElementById("inputBytes").value = valNum * 1024;
                document.getElementById("inputMegabytes").value = valNum / 1024;
                document.getElementById("inputGigabytes").value = valNum / 1048576;
                document.getElementById("inputTerabytes").value = valNum / 1099511627776;
            }
            if (selected && selected.id == "inputMegabytes") {
                document.getElementById("inputBytes").value = valNum * 1048576;
                document.getElementById("inputKilobytes").value = valNum * 1024;
                document.getElementById("inputGigabytes").value = valNum / 1024;
                document.getElementById("inputTerabytes").value = valNum / 1048576;
            }
            if (selected && selected.id == "inputGigabytes") {
                document.getElementById("inputBytes").value = valNum * 1073741824;
                document.getElementById("inputKilobytes").value = valNum * 1048576;
                document.getElementById("inputMegabytes").value = valNum * 1024;
                document.getElementById("inputTerabytes").value = valNum / 1024;
            }
            if (selected && selected.id == "inputTerabytes") {
                document.getElementById("inputBytes").value = valNum * 1099511627776;
                document.getElementById("inputKilobytes").value = valNum * 1073741824;
                document.getElementById("inputMegabytes").value = valNum * 1048576;
                document.getElementById("inputGigabytes").value = valNum * 1024;
            }
        }

This is the heart of it, the core, the function!!! Yes, I am sure there are some of you looking at the above code in horror because there are way more efficient ways of doing this math. Baby steps right? Basically, it’s a bunch of if statements that get triggered depending on the field the user clicked on. It was really important to me to have the number automatically update as I entered what I wanted to see converted. This was a must. I already have a good understanding of bits & bytes and converting them mentally or with a calculator (realistically I just open an Excel workbook…). Since computers and scripting are a thing, these days I usually just use an online converter. So I thought this would be the perfect beginners JavaScript project, something that is of use to me.

What’s Next?

My mental “roadmap” if you will for this will be to eventually include conversions to decimal (1 Megabyte = 1000000 bytes). Then probably fix the margins a little on mobile devices.

After that, probably more responsive CSS, cleaner UI, wider browser support. I’d also really like to have a loader that displays a fun fact about data sizes.

Share the Post:

Related Posts

The Infinite Forbidden Set Stats

Yu-Gi-Oh! The Infinite Forbidden launched in the TCG on July 19, 2024. I’ve been excited for this set since its announcement because of the nostalgia-bait of Exodia. As a core set, it’s full of new cards, strategies, and archetypes. The highly anticipated Fiendsmith makes its TCG debut just in time for the Yu-Gi-Oh! NAWCQ. Exodia, White Forest, and Gimmick Puppet get support, and a new TCG-exclusive archetype called Mimighoul makes its debut.

Read More

Help! I’m Missing haptic feedback in Beat Saber

The haptic feedback in Beat Saber just stopped working. I knew it wasn’t the controller since vibrations were working outside of Steam or Beat Saber. I didn’t realize how much the haptic feedback contributed to VR immersion, but wow, did I miss it.

Read More