Conversion functions help computer programs to change information like numbers or letters from one type to another. For example, we can use conversion functions to change the number "10" into its binary equivalent "1010". Conversion functions are important because computers work with different types of information and formats, and sometimes they need to change one type of information into another to perform a specific task or operation.
In order to convert those types we need tools like built-in functions to help us. More specifically the built in functions: parseInt(), toString(), charCodeAt(), String.fromCharCode(), and substring(). Before I show you some examples of the usage, let me explain what binary, hexadecimal, decimal, & ascii are as well as what exactly the built-in functions do.
What are binary, hexadecimal, decimal, & ascii?
Binary (also known as Base-2) is a numbering scheme in which there are only two possible values for each digit, 0 or 1 and these are also the values that computers happen to be stored in. Binary numbers are often represented with 0b at the beginning.
0b11001010
side note: The word bits is short for binary digits and a group of 8 bits is known as a byte.
Hexadecimal (also known as Base-16) is often used as shorthand for representing binary values: one hex digit can represent four bits. The digits are 0-9 with A-F representing 10-15. A hexadecimal is often represented with 0x at the beginning.
0xF23C
Decimal (also known as Base-10) is the most commonly used system. It uses 10 symbols (0-9), to represent numbers.
ASCII (which is short for American Standard Code for Information Interchange) is a character encoding system that assigns unique numerical codes to represent a set of characters used in computers. It's a seven-bit code meaning it can represent up to 128 characters, including numbers, letters, punctuation marks, and control codes.
ASCII CODE FOR "B" => 01000010 in binary
Built-in functions: parseInt(), toString(), charCodeAt(), String.fromCharCode(), and substring()
parseInt() is a function that parses a string argument and returns an integer of the specified radix (the radix or base is the number of unique digits, including the digit zero, used to represent numbers.)
parseInt(string, radix)
toString() returns a string representing the object
let number = 1;
number.toString();
=> "1"
But it can also be used to convert decimals into binary or hexadecimal by taking the radix that specifies the base of the number system to be used for conversion.
binary: base-2 => radix = 2 | hexadecimal : base-16 => radix = 16 | decimal: base-10 => radix = 10
example:
let number = 255;
let hex = number.toString(16);
=> "ff"
charCodeAt() returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.
function charGreeting(greeting) {
let greetCode = [];
for(let i = 0; i < greeting.length; i++) {
greetCode.push(greeting.charCodeAt(i))
}
return greetCode;
}
charGreeting("Hey")
=> [104, 101, 121]
H => 104
e => 101
y => 121
String.fromCharCode returns a string created from the specified sequence of UTF-16 code units.
function charGreeting(charCode) {
let greeting = [];
for(let i = 0; i < charCode.length; i++) {
greeting.push(String.fromCharCode(charCode[i]))
}
return greeting.join("");
}
stringCharCode([104, 101, 121]
=> "hey"
Substring returns the part of the string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied.
let name = "Tatiana"
name.substring(3)
=> "iana"
Conversion Function Examples & Breakdowns
converting binary and hexadecimal to decimals
const convertingBinarytoDecimal = binary => {
let binaryRemoval = binary.substring(2)
let decimal = parseInt(binaryRemoval, 2)
return decimal;
}
convertingBinarytoDecimal('0b0101')
=> 5
First the function removes the first two characters of the binary string, which are the prefix 0b, and stores the resulting string in the variable binaryRemoval.
The function then uses the parseInt function to convert the binaryRemoval string to a decimal number. The 2 radix (base) is passed as the second argument to parseInt, indicating that the input is a binary number. The resulting decimal value is stored in the variable decimal.
Finally, the function returns the decimal value.
note: something you will notice is that converting binary and hexadecimal are done the exact same way, the only differences are the base numbers.
const convertingHexToDecimal = hexadecimal => {
let hexRemoval = hexidecimal.substring(2)
let decimal = parseInt(hexRemoval, 16)
return decimal;
}
convertingHexToDecimal('0x51')
=> 81
converting decimals to hexadecimal and binary
const convertingDecimalToBinary = decimal => {
let binary = decimal.toString(2)
return `0b${binary}`
}
convertingDecimalToBinary(13)
=> '0b1101'
First, the function converts the decimal number to a binary string by calling the toString method on the decimal variable with a 2 radix (base) as the argument. The resulting binary string is stored in the variable binary.
Finally, the function concatenates the string 0b with the binary string and returns the resulting string, which represents the binary equivalent of the original decimal number.
function convertingDecimalToHexadecimal(decimal) {
let hex = decimal.toString(16)
return `0x${hex}`
}
convertingDecimalToHexadecimal(9)
=> '0x9'
converting binary to hexadecimal and hexadecimal to binary
const convertingBinaryToHex = binary => {
let binaryRemoval = binary.substring(2)
let decimal = parseInt(binaryRemoval, 2)
let hex = decimal.toString(16)
return `0x${hex}`
}
convertingBinaryToHex('0b1010')
=> '0xa'
The function starts by removing the first two characters of the binary (0b) and stores the resulting string in the variable binaryRemoval. Then converts the binary number to decimal by calling the parseInt function with the binaryRemoval string as the first argument and the 2 radix (base) as the second argument. The resulting decimal number is stored in the variable decimal.
Next, the function converts the decimal number to hexadecimal by calling the toString function on the decimal variable with the 16 radix as the argument. The resulting hexadecimal string is stored in the variable hex.
Finally, the function concatenates the string 0x with the hex string and returns the resulting string, which represents the hexadecimal equivalent of the original binary number.
const convertingHextoBinary = hex => {
let hexRemoval = hex.substring(2)
let decimal = parseInt(hexRemoval, 16)
let binary = decimal.toString(2)
return `0b${binary}`
}
convertingHextoBinary('0xa1')
=> '0b10100001'
converting binary and hexadecimal to ASCII
const convertingBinarytoASCII = (binary) => {
const binaryPrefixRemoval = binary.substring(2)
const decimal = parseInt(binaryPrefixRemoval, 2)
return String.fromCharCode(decimal)
}
convertingBinarytoASCII("0b01000010")
=> B
First, the function removes the first two characters of the binary string, which are the prefix 0b, and stores the resulting string in the variable binaryPrefixRemoval.
The function then converts the binary number to decimal by calling the parseInt function with the binaryPrefixRemoval string as the first argument and the 2 radix (base) as the second argument. The resulting decimal number is stored in the variable decimal.
Finally, the function uses the String.fromCharCode method to convert the decimal value to its corresponding ASCII character. The ASCII character is returned by the function.
const convertingHextoASCII = (hex) => {
const hexPrefixRemoval = hex.substring(2)
const decimal = parseInt(hexPrefixRemoval, 16)
return String.fromCharCode(decimal)
}
convertingHextoASCII("0x43")
=> C
converting decimal to ASCII and ASCII to decimal
const convertingDecimalToAscii = decimal => {
return String.fromCharCode(decimal)
}
convertingDecimalToAscii(65)
=> "A"
The function uses the String.fromCharCode method to convert the decimal value to its corresponding ASCII character. The ASCII character is returned by the function.
const convertingAsciiToDecimal= ASCII => {
let decimals = []
for(let i = 0; i < ASCII.length; i++) {
decimals.push(ASCII.charCodeAt(i))
}
return decimals
}
convertingAsciiToDecimal('HELLO'))
=> [ 72, 69, 76, 76, 79 ]
The function creates an empty array called decimals then using a for loop, the function iterates over each character of the ASCII string, one at a time. For each character, the function calls the charCodeAt method, passing the index of the character in the string as an argument. This method returns the Unicode value of the character, which corresponds to the decimal value of the ASCII character. The decimal value is then added to the decimals array using the push method.
After iterating over all characters in the ASCII string, the function returns the decimals array, which contains the decimal values of each ASCII character in the original string.
Conclusion
In conclusion, conversion functions are crucial for converting between different data types and formats, including binary, hexadecimal, decimal, ASCII, and others. Being able to convert between these formats is essential for data manipulation, encoding, and decoding. Understanding how to use these conversion functions is fundamental for all developers and can improve their coding efficiency and versatility.
Top comments (0)