char2num {lazy.utf8} | R Documentation |
Convert a Character Vector to a List of Numeric Vectors or vice versa via raw expression.
char2num(char) num2char(num)
char |
a character vector consisting of strings |
num |
a list of numeric vectors or a numeric vector which can be converted to string via raw conversion. |
Since these functions can handle multi byte characters,
they can be used to store the strings with double byte characters
as a list of numeric vectors.
The core part of these functions are:
char to num
num <- as.numeric( charToRaw(char) )
num to char
char <- rawToChar( as.raw( num ) )
The numbers and Raw expression are related as
num <- char2num( char ) ( hex <- as.hexmode( num ) ) print( as.raw(num) )
char2num
If char is a string ( a vector of length 1 ), num is a vector.
Otherwise, num is a list of length(char)
num2char
If num is a vector, char is a string.
Otherwise, char is a vector of length(num)
# char is a string ( char <- "This is an example." ) ( num <- char2num( char ) ) ( is.numeric(num) ) ( char2 <- num2char( num ) ) # char is a string vector ( char2 <- c("This is an example.", "2nd line") ) ( num2 <- char2num( char2 ) ) ( sapply( num2, is.numeric ) ) ( char22 <- num2char( num2 ) ) # storing multi byte characters in a package: \cr # See the description of 'generate_zenkaku_sets'. {if( regexpr("utf8",Sys.getlocale (category = "LC_CTYPE")) > 0 ) nn <- c(87, 101, 32, 104, 97, 118, 101, 32, 228, 184, 141, 232, 184 , 176, 231, 159, 169, 32, 121, 101, 116, 33) else nn <- c(87, 101, 32, 104, 97, 118, 101, 32, 149, 115, 230, 250, 139 , 233, 32, 121, 101, 116, 33)} ( zz <- num2char(nn) ) ( zzh <- as.hexmode(nn) ) charToRaw(zz)