Open In App

R Program to Convert Decimal to Binary, Octal, and Hexadecimal

Last Updated : 01 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Converting decimal numbers to binary, octal, and hexadecimal is a fundamental operation in R Programming Language. These different numeral systems are used to represent numbers in other bases. In this article, we’ll create programs to convert decimal to binary, octal & hexadecimal.

Concepts related to the topic

There are some following concepts that are related to this topic.

Decimal Number System (Base 10)

The decimal number system, also known as base-10, is the most common numeral system used in everyday life. It consists of 10 digits, 0 through 9.

Example of Decimal Number:  1234

This is a standard representation of a number in base-10.

Binary Number System (Base 2)

The binary number system is composed of only two digits, 0 and 1, making it a base-2 system.

Decimal: 10
Binary: 1010

To convert 10 to binary, you can use the process of successive division by 2. The remainder at each step is written down, and the binary representation is obtained by reading these remainders from bottom to top. In this case, 10 in binary is 1010.

Octal Number System (Base 8)

The octal number system uses 8 digits, 0 through 7, making it a base-8 system

Decimal: 63
Octal: 77

To convert 63 to octal, you can use the process of successive division by 8. The remainder at each step is written down, and the octal representation is obtained by reading these remainders from bottom to top. In this case, 63 in octal is 77.

Hexadecimal Number System (Base 16)

The hexadecimal number system is a base-16 system, using digits 0-9 and letters A-F (or a-f) to represent values from 10 to 15.

Decimal: 255
Hexadecimal: FF

To convert 255 to hexadecimal, you can use the process of successive division by 16. The remainder at each step is written down, and the hexadecimal representation is obtained by reading these remainders from bottom to top. In this case, 255 in hexadecimal is FF.

Now, let’s provide detailed explanations and examples for each of the conversion methods:

Approach 1: Using Built-in Functions

In R Programmingf Language We have Built-in Functions for these operations.

Convert Decimal to Binary

R




decimal_num <- 75
binary_num <- as.integer(intToBits(decimal_num))
cat("Decimal to Binary:", paste(rev(binary_num), collapse = ""), "\n")


Output:

Decimal to Binary: 00000000000000000000000001001011 
  • decimal_num <- 33: Assigns the decimal number 33 to the variable decimal_num.
  • binary_num <- as.integer(intToBits(decimal_num)): Converts the decimal number into its binary representation and stores it as an integer in the variable binary_num.
  • cat(“Decimal to Binary:”, paste(rev(binary_num), collapse = “”), “\n”): Prints the message “Decimal to Binary:” followed by the binary representation of 33 by reversing the binary digits and removing spaces, separated by a newline.

Convert Decimal to Octal

R




# Convert Decimal to Octal
decimal_num <- 1024
octal_num <- sprintf("%o", decimal_num)
cat("Decimal to Octal:", octal_num, "\n")


Output:

Decimal to Octal: 2000
  • decimal_num <- 128: Assigns the decimal number 128 to the variable decimal_num.
  • octal_num <- sprintf(“%o”, decimal_num): Converts the decimal number into its octal representation using the sprintf function with the “%o” format specifier and stores it in the variable octal_num.
  • cat(“Decimal to Octal:”, octal_num, “\n”): Prints the message “Decimal to Octal:” followed by the octal representation of 128 and a newline character.

Convert Decimal to Hexadecimal

R




# Convert Decimal to Hexadecimal
decimal_num <- 255
hex_num <- sprintf("%X", decimal_num)
cat("Decimal to Hexadecimal:", hex_num, "\n")


Output:

Decimal to Hexadecimal: FF 
  • decimal_num <- 255: Assigns the decimal number 255 to the variable decimal_num.
  • hex_num <- sprintf(“%X”, decimal_num): Converts the decimal number into its hexadecimal representation using the sprintf function with the “%X” format specifier and stores it in the variable hex_num.
  • cat(“Decimal to Hexadecimal:”, hex_num, “\n”): Prints the message “Decimal to Hexadecimal:” followed by the hexadecimal representation of 255 and a newline character.

Approach 2: Manual Conversion

we can do the all the operations through manually also.

Convert Decimal to Binary

R




# Manual Conversion: Decimal to Binary
decimal_to_binary <- function(decimal_num) {
    binary_num <- character(0)
    while (decimal_num > 0) {
        remainder <- decimal_num %% 2
        binary_num <- c(remainder, binary_num)
        decimal_num <- decimal_num %/% 2
    }
    return(paste(binary_num, collapse = ""))
}
 
decimal_num <- 75
cat("Decimal to Binary:", decimal_to_binary(decimal_num), "\n")


Output:

Decimal to Binary: 1001011 
  • decimal_to_binary <- function(decimal_num): Defines a custom function that takes a decimal number as input.
  • binary_num <- character(0): Initializes an empty character vector to store the binary digits.
  • Inside a while loop:
  • remainder <- decimal_num %% 2: Calculates the remainder of dividing the decimal number by 2, which represents the binary digit.
  • binary_num <- c(remainder, binary_num): Prepends the remainder to the binary_num vector.
  • decimal_num <- decimal_num %/% 2: Updates the decimal number by performing integer division by 2.
  • return(paste(binary_num, collapse = “”)): Returns the binary representation as a concatenated string.
  • decimal_num <- 75: Assigns the decimal number 75 to the variable decimal_num.
  • cat(“Decimal to Binary:”, decimal_to_binary(decimal_num), “\n”): Calls the custom function to convert 75 to binary and prints “Decimal to Binary:” followed by the binary representation and a newline character.

Convert Decimal to Octal

R




# Manual Conversion: Decimal to Octal
decimal_to_octal <- function(decimal_num) {
    octal_num <- character(0)
    while (decimal_num > 0) {
        remainder <- decimal_num %% 8
        octal_num <- c(remainder, octal_num)
        decimal_num <- decimal_num %/% 8
    }
    return(paste(octal_num, collapse = ""))
}
 
decimal_num <- 1024
cat("Decimal to Octal:", decimal_to_octal(decimal_num), "\n")


Output:

Decimal to Octal: 2000 
  • decimal_to_octal <- function(decimal_num): Defines a custom function that takes a decimal number as input.
  • octal_num <- character(0): Initializes an empty character vector to store the octal digits.
  • Inside a while loop:
  • remainder <- decimal_num %% 8: Calculates the remainder of dividing the decimal number by 8, which represents an octal digit.
  • octal_num <- c(remainder, octal_num): Prepends the remainder to the octal_num vector.
  • decimal_num <- decimal_num %/% 8: Updates the decimal number by performing integer division by 8.
  • return(paste(octal_num, collapse = “”)): Returns the octal representation as a concatenated string.
  • decimal_num <- 1024: Assigns the decimal number 1024 to the variable decimal_num.
  • cat(“Decimal to Octal:”, decimal_to_octal(decimal_num), “\n”): Calls the custom function to convert 1024 to octal and prints “Decimal to Octal:” followed by the octal representation and a newline character.

Convert Decimal to Hexadecimal

R




# Manual Conversion: Decimal to Hexadecimal
decimal_to_hexadecimal <- function(decimal_num) {
    hexadecimal_num <- character(0)
    hex_digits <- c(0:9, "A", "B", "C", "D", "E", "F")
     
    while (decimal_num > 0) {
        remainder <- decimal_num %% 16
        hexadecimal_num <- c(hex_digits[remainder + 1], hexadecimal_num)
        decimal_num <- decimal_num %/% 16
    }
    return(paste(hexadecimal_num, collapse = ""))
}
 
decimal_num <- 255
cat("Decimal to Hexadecimal:", decimal_to_hexadecimal(decimal_num), "\n")


Output:

Decimal to Hexadecimal: FF
  • decimal_to_hexadecimal <- function(decimal_num): Defines a custom function that takes a decimal number as input.
  • hexadecimal_num <- character(0): Initializes an empty character vector to store the hexadecimal digits.
  • hex_digits <- c(0:9, “A”, “B”, “C”, “D”, “E”, “F”): Defines an array of hexadecimal digits.
  • Inside a while loop:
  • remainder <- decimal_num %% 16: Calculates the remainder of dividing the decimal number by 16, representing a hexadecimal digit.
  • hexadecimal_num <- c(hex_digits[remainder + 1], hexadecimal_num): Prepends the corresponding hexadecimal digit from the array to the hexadecimal_num vector.
  • decimal_num <- decimal_num %/% 16: Updates the decimal number by performing integer division by 16.
  • return(paste(hexadecimal_num, collapse = “”)): Returns the hexadecimal representation as a concatenated string.
  • decimal_num <- 42: Assigns the decimal number 42 to the variable decimal_num.
  • cat(“Decimal to Hexadecimal:”, decimal_to_hexadecimal(decimal_num), “\n”): Calls the custom function to convert 42 to hexadecimal and prints “Decimal to Hexadecimal:” followed by the hexadecimal representation and a newline character.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads