28

I need to translate a string of characters, for example "Hello", into a string of numbers which is the ASCII numeric codes.

Example: 0 -> 48; a -> 97, etc.

Does anyone know an R function to do this? Hopefully, the function or piece of code will translate "Hello" into a numeric string like

c(72, 101, 108, 108, 111)
smci
  • 32,567
  • 20
  • 113
  • 146
Ender
  • 297
  • 1
  • 3
  • 7

1 Answers1

46

I guess you mean utf8ToInt, see the R manuals:

utf8ToInt("Hello")
# [1]  72 101 108 108 111

Or, if you want a mapping of the letters to their codes:

sapply(strsplit("Hello", NULL)[[1L]], utf8ToInt)
#  H   e   l   l   o 
# 72 101 108 108 111 
zx8754
  • 52,746
  • 12
  • 114
  • 209
PhilMasteG
  • 3,095
  • 1
  • 20
  • 27