AscToChar returns a character for each ASCII code (integer) supplied.
CharToAsc returns integer codes in 0:255 for each (one byte) character in all strings in x.

AscToChar(i)
CharToAsc(x)

Arguments

i

numeric (integer) vector of values in 1:255.

x

vector of strings.

Details

Only codes in 1:127 make up the ASCII encoding which should be identical for all R versions, whereas the ‘upper’ half is often determined from the ISO-8859-1 (aka “ISO-Latin 1)” encoding, but may well differ, depending on the locale setting, see also Sys.setlocale.

Note that 0 is no longer allowed since, R does not allow \0 aka nul characters in a string anymore.

Value

AscToChar returns a vector of the same length as i. CharToAsc returns a list of numeric vectors of character length of each string in x.

Author

unknown guy out there, help text partly taken from M. Maechler's sfsmisc.

See also

Examples

(x <- CharToAsc("Silvia"))
#> [1]  83 105 108 118 105  97

# will be pasted together
AscToChar(x)
#> [1] "Silvia"

# use strsplit if the single characters are needed
strsplit(AscToChar(x), split=NULL)
#> [[1]]
#> [1] "S" "i" "l" "v" "i" "a"
#> 

# this would be an alternative, but the latter would be of class raw
DecToHex(CharToAsc("Silvia"))
#> [1] "53" "69" "6c" "76" "69" "61"
charToRaw("Silvia")
#> [1] 53 69 6c 76 69 61