Check out a free preview of the full Web Assembly (Wasm) course
The "Convert Hex & Binary with toString" Lesson is part of the full, Web Assembly (Wasm) course featured in this preview video. Here's what you'd learn in this lesson:
Jem demonstrates how the toString method in JavaScript can be passed an optional parameter called a radix which represents the base value for converting numbers to binary, decimal, or hexadecimal. The prefix "0x" is used to write hexadecimal values.
Transcript from the "Convert Hex & Binary with toString" Lesson
[00:00:00]
>> We understand how to read Hex. We can convert it easily. I know, I'm sorry, I tricked you. I made you go through the exercise where you're probably writing it down by hand. We can do this using programming because we're programmers, that's what we do. We're lazy. We don't wanna have to memorize how to convert things.
[00:00:17]
And we can do this with totally underrated toString method in JavaScript, we do this in the browser. What most people don't know, and honestly I didn't know for a long time, was that if we use object prototype toString, we can pass it a radix. And the radix, just think of it as base.
[00:00:38]
The radix is like a fancy term of saying base. And we understood from before that base 2, base 10, base 16, that's what it means. We can pass the radix to the toString and JavaScript will convert that into whatever format we want. So, there's an exercise here, but I'll do the first one for you.
[00:00:59]
Or we'll go through the first one. So if we want to run a function that converts hex to decimal. And decimal being our modern number system that we all know and love, we can just create a function called hexadecimal. It's gonna take a hex in, and then we're gonna pass it to the toString method with a base 10.
[00:01:17]
And actually, let's fire that up. Let's see if if I'm lying or not. You never know. I'm just gonna copy it, cuz, lazy, I think I've mentioned that before. And what was the number we just did text to decimal? What are we trying to convert it was,
>> 2f7.
[00:01:43]
>> 2f7, thank you. So we can't just write 2f7, we have to tell JavaScript that this is hex and not just a random miss type string. So we're gonna say 0x, which means it's a hexadecimal number, and 2f7 I did it wrong. 0, there we go. I typed an O instead of a 0.
[00:02:11]
It's a common mistake I make. So we see that converts to a 759. I don't think it's 2x7, was it?
>> I must have said the wrong number.
>> 2e7,
>> Sorry.
>> Trying to make me look bad.
>> [INAUDIBLE] [LAUGH]
>> [LAUGH] I was like, did I get my math wrong, which happens a lot.
[00:02:28]
743, so that's easy. Now in the future you can write your own utility methods if you really wanna nerd out, which I encourage you to do at maximum nerd volume. You can write your own methods convert to and from different formats. So, without cheating, write a function that converts decimal into binary.
[00:02:49]
And to me this is really gets really interesting cuz you actually see what numbers represent in 0s and 1s. And I should mention for the exercises, yeah, you can copy paste code, but in all of my years of learning, I have discovered that I learn and I think most people learn when they actually write it down.
[00:03:10]
There's something about the muscle brain connection, I forget what it's called in actually like kinesiology. But there's something about your muscles doing something, your fingers doing something, and your brain comprehending that, that makes it stick more than if you just copy and pasted like yeah, that was pretty simple, I did it.
[00:03:32]
But I really encourage you to actually do the exercises and not just copy paste them or look at them. I promise it's seems like the hard way, but you will learn a lot better when you code it yourself. Yeah, it's the same principle as before, we're just passing a different radix to the toString method.
[00:03:50]
So, I'm not gonna take my own advice and copy paste. So we'll say decimal to binary, and I wanna see what's 22 is. And binary, and it's 0110. Actually, there are a bunch more leading zeros, but because they're not terribly relevant and just makes it harder to read, JavaScript just cuts us off.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops