What is the charToRaw() function in R?

Overview

The charToRaw() function in R is used to return a given character’s ASCII value.

charToRaw(x)

Parameter value

The charToRaw() function takes a single and mandatory parameter value x, representing the character that we want to convert to the ASCII value.

Return value

The charToRaw() function returns the ASCII value of the character passed to it.

Code example 1

We will call the charToRaw() function over some letters in the code below:

# creating our character variables
a <- charToRaw("a")
b <- charToRaw("b")
c <- charToRaw("c")
d <- charToRaw("d")
e <- charToRaw("ab")
# Their ASCII values
a
b
c
d
e

Code explanation

  • Lines 2-6: We call the charToRaw() function over the characters we created.
  • Line 9-13: We print the variables containing the ASCII values that correspond to the characters we passed to the function.

Code example 2

We will create string characters in the code below and determine their respective ASCII values.

# creating our string variables
a <- "R"
b <- "R language is the best!"
# getting their ASCII values
charToRaw(a)
charToRaw(b)

Code explanation

  • Lines 2-3: We create string variables a and b.
  • Lines 6-7: We call the charToRaw() function over the string variables.

Free Resources