| 1 |
\name{AsciiToInt} |
| 2 |
\alias{AsciiToInt} |
| 3 |
\alias{ichar} |
| 4 |
\alias{chars8bit} |
| 5 |
\alias{strcodes} |
| 6 |
\title{Character to and from Integer Codes Conversion} |
| 7 |
\description{ |
| 8 |
\code{AsciiToInt} returns \code{\link{integer}} codes in \code{0:255} |
| 9 |
for each (one byte) character in \code{strings}. \code{ichar} is an |
| 10 |
alias for it, for old S compatibility. |
| 11 |
|
| 12 |
\code{strcodes} implements in \R the basic engine for translating |
| 13 |
characters to corresponding integer codes. |
| 14 |
|
| 15 |
\code{chars8bit()} is the \emph{inverse} function of |
| 16 |
\code{AsciiToint}, producing \dQuote{one byte} characters from integer |
| 17 |
codes. Note that it (and hence \code{strcodes()} depends on the |
| 18 |
locale, see \code{\link{Sys.getlocale}()}. |
| 19 |
} |
| 20 |
\usage{ |
| 21 |
AsciiToInt(strings) |
| 22 |
ichar(strings) |
| 23 |
chars8bit(i = 1:255) |
| 24 |
strcodes(x, table = chars8bit(1:255)) |
| 25 |
} |
| 26 |
\arguments{ |
| 27 |
\item{strings, x}{\code{\link{character}} vector.} |
| 28 |
\item{i}{numeric (integer) vector of values in \code{1:255}.} |
| 29 |
\item{table}{a vector of (unique) character strings, typically of one |
| 30 |
character each.} |
| 31 |
} |
| 32 |
\details{ |
| 33 |
Only codes in \code{1:127} make up the ASCII encoding which should be |
| 34 |
identical for all \R versions, whereas the \emph{\sQuote{upper}} half |
| 35 |
is often determined from the ISO-8859-1 (aka \dQuote{ISO-Latin 1)} |
| 36 |
encoding, but may well differ, depending on the locale setting, see |
| 37 |
also \code{\link{Sys.setlocale}}. |
| 38 |
|
| 39 |
Note that \code{0} is no longer allowed since, \R does not allow |
| 40 |
\code{\\0} aka \code{nul} characters in a string anymore. |
| 41 |
} |
| 42 |
\value{ |
| 43 |
\code{AsciiToInt} (and hence \code{ichar}) and \code{chars8bit} return a |
| 44 |
vector of the same length as their argument. |
| 45 |
|
| 46 |
\code{strcodes(x, tab)} returns a \code{\link{list}} of the same |
| 47 |
\code{\link{length}} and \code{\link{names}} as \code{x} with list |
| 48 |
components of integer vectors with codes in \code{1:255}. |
| 49 |
} |
| 50 |
\author{Martin Maechler, partly in 1991 for S-plus} |
| 51 |
\examples{ |
| 52 |
chars8bit(65:70)#-> "A" "B" .. "F" |
| 53 |
stopifnot(identical(LETTERS, chars8bit(65:90)), |
| 54 |
identical(AsciiToInt(LETTERS), 65:90)) |
| 55 |
|
| 56 |
% In R 2.1.0, the "ü" could not even be parsed in UTF-8; now gives NA |
| 57 |
## may only work in ISO-latin1 locale (not in UTF-8): |
| 58 |
try( strcodes(c(a= "ABC", ch="1234", place = "Zürich")) ) |
| 59 |
## in "latin-1" gives {otherwise should give NA instead of 252}: |
| 60 |
\dontrun{ |
| 61 |
$a |
| 62 |
[1] 65 66 67 |
| 63 |
|
| 64 |
$ch |
| 65 |
[1] 49 50 51 52 |
| 66 |
|
| 67 |
$place |
| 68 |
[1] 90 252 114 105 99 104 |
| 69 |
} |
| 70 |
|
| 71 |
myloc <- Sys.getlocale() |
| 72 |
|
| 73 |
if(.Platform $ OS.type == "unix") withAutoprint({ # ''should work'' here |
| 74 |
try( Sys.setlocale(locale = "de_CH") )# "try": just in case |
| 75 |
strcodes(c(a= "ABC", ch="1234", place = "Zürich")) # no NA hopefully |
| 76 |
AsciiToInt(chars8bit()) # -> 1:255 {if setting latin1 succeeded above} |
| 77 |
|
| 78 |
chars8bit(97:140) |
| 79 |
try( Sys.setlocale(locale = "de_CH.utf-8") )# "try": just in case |
| 80 |
chars8bit(97:140) ## typically looks different than above |
| 81 |
}) |
| 82 |
|
| 83 |
## Resetting to original locale .. works "mostly": |
| 84 |
lapply(strsplit(strsplit(myloc, ";")[[1]], "="), |
| 85 |
function(cc) try(Sys.setlocale(cc[1], cc[2]))) -> .scratch |
| 86 |
|
| 87 |
Sys.getlocale() == myloc # TRUE if we have succeeded to reset it |
| 88 |
} |
| 89 |
\keyword{manip} |
| 90 |
|