% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/UTF8pgm.R
\name{nbyte}
\alias{nbyte}
\title{Returns the number of bytes used to represent each character
in each of the string.}
\usage{
nbyte(x)
}
\arguments{
\item{x}{String vector}
}
\value{
a list consisting of the number of bytes per character
}
\description{
Returns the number of bytes used to represent each character
in each of the string.
}
\details{
Let x = "abc". The core of this function is \cr
\code{lapply(
  strsplit(x,""), function(y) unlist(lapply(lapply(y,charToRaw),length))
  ) } \cr
It counts the length of the Raw Expression of each character.

About rawToChar and charToRaw functions: \cr
To get the raw expression of character, use\cr
\code{ raw <- charToRaw(paste(c(0:9," ",letters,LETTERS),collapse="")) }\cr
To get the numeric equivalent of raw, use\cr
\code{ num <- as.numeric(raw) }.\cr
To recover the original characters, use\cr
\code{ char <- rawToChar( as.raw(num) ) }.\cr
}
\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))


}
