nbyte {lazy.tools}R Documentation

Returns the number of bytes used to represent each character in each of the string.

Description

Returns the number of bytes used to represent each character in each of the string.

Usage

nbyte(x)

Arguments

x

String vector

Details

Let x = "abc". The core of this function is
lapply( strsplit(x,""), function(y) unlist(lapply(lapply(y,charToRaw),length)) )
It counts the length of the Raw Expression of each character.

About rawToChar and charToRaw functions:
To get the raw expression of character, use
raw <- charToRaw(paste(c(0:9," ",letters,LETTERS),collapse=""))
To get the numeric equivalent of raw, use
num <- as.numeric(raw) .
To recover the original characters, use
char <- rawToChar( as.raw(num) ) .

Value

a list consisting of the number of bytes per character

Examples

# Example of one, two, three amd four byte characters on UTF-8 machines
if( regexpr("utf8",Sys.getlocale (category = "LC_CTYPE")) > 0 ){
 num=c(97, 206, 177, 227, 129, 130, 240, 160, 174, 183)
 char=num2char(num)
 nbyte(char)
}

# A string consisting of single and multi byte characters
# The 3rd, 6th and 7th characters are uppercase Zenkaku Romans.
{if( regexpr("utf8",Sys.getlocale (category = "LC_CTYPE")) > 0 )
 nza <- c(97, 98, 239, 188, 163, 100, 101, 239, 188, 166, 239, 188, 167, 104)
else
 nza <- c(97,98,130,98,100,101,130,101,130,102,104)}
za <- rawToChar(as.raw(nza))

# location of the multi byte characters
nchar(za)
is.DBCS(za)
printm( nchar(za), length(charToRaw(za)), nchar2(za) )
sza=unlist(strsplit(za,""))
printm(sza,nbyte(za),is.DBCS(za))

cat("When using Shif-JIS encoding, ",
    "it seems that 'strsplit' makes the result 3 byte characters.\n")
printm(sza,unlist(nbyte(sza)),unlist(is.DBCS(sza)))
psza <- paste(sza,collapse="")
printm(sza,nbyte(psza),is.DBCS(za))

identical(za,psza)
identical(nbyte(za),nbyte(psza))

# Instead of strsplit, use below
sza2 <- substring(za,1:nchar(za),1:nchar(za))
printm( sza, unlist( nbyte( sza2 ) ) )
psza2 <- paste(sza2,collapse="")
nbyte(psza2)

identical(za,psza2)
identical(nbyte(za),nbyte(psza2))



[Package lazy.tools version 0.1.4 ]