Last data update: 2014.03.03

R: Convert Characters to ASCII
ASCIIfyR Documentation

Convert Characters to ASCII

Description

Convert character vector to ASCII, replacing non-ASCII characters with single-byte (x00) or two-byte (u0000) codes.

Usage

ASCIIfy(x, bytes = 2, fallback = "?")

Arguments

x

a character vector, possibly containing non-ASCII characters.

bytes

either 1 or 2, for single-byte (x00) or two-byte (u0000) codes.

fallback

an output character to use, when input characters cannot be converted.

Value

A character vector like x, except non-ASCII characters have been replaced with x00 or u0000 codes.

Note

To render single backslashes, use these or similar techniques:

    write(ASCIIfy(x), "file.txt")
    cat(paste(ASCIIfy(x), collapse="\n"), "\n", sep="")

The resulting strings are plain ASCII and can be used in R functions and datasets to improve package portability.

Author(s)

Arni Magnusson arnima@hafro.is

See Also

showNonASCII identifies non-ASCII characters in a character vector.

Examples

cities <- c("Su00e3o Paulo", "Reykjavu00edk")
print(cities)
ASCIIfy(cities, 1)
ASCIIfy(cities, 2)

athens <- "u0391u03b8u03aeu03bdu03b1"
print(athens)
ASCIIfy(athens)

Results