Debian Codegraph @

man/digitsBase.Rd

raw
1 \name{digitsBase}
2 \alias{digitsBase}
3 \alias{as.intBase}
4 \alias{bi2int}
5 \alias{as.integer.basedInt}
6 \alias{print.basedInt}
7 \title{Digit/Bit Representation of Integers in any Base}
8 \description{
9 Integer number representations in other Bases.
10
11 Formally, for every element \eqn{N =}\code{x[i]}, compute the (vector
12 of) \dQuote{digits} \eqn{A} of the \code{base} \eqn{b}
13 representation of the number \eqn{N}, \eqn{N = \sum_{k=0}^M A_{M-k} b ^ k}{%
14 N = sum(k = 0:M ; A[M-k] * b^k)}.\cr
15 Revert such a representation to integers.
16 }
17 \usage{
18 digitsBase(x, base = 2, ndigits = 1 + floor(1e-9 + log(max(x,1), base)))
19 \method{as.integer}{basedInt}(x, \dots)
20 \method{print}{basedInt}(x, \dots)
21
22 as.intBase(x, base = 2)
23 bi2int(xlist, base)
24 }
25 \arguments{
26 \item{x}{For \code{digitsBase()}: non-negative integer (vector) whose
27 base \code{base} digits are wanted.
28
29 For \code{as.intBase()}: \cr a list of numeric vectors, a character
30 vector, or an integer matrix as returned by \code{digitsBase()},
31 representing digits in base \code{base}.
32 }
33 \item{base}{integer, at least 2 specifying the base for representation.}
34 \item{ndigits}{number of bits/digits to use.}
35 \item{\dots}{potential further arguments passed to methods, notably
36 \code{\link{print}}.}
37 \item{xlist}{a \code{\link{list}} of integer vectors with entries
38 typically in \code{0:(base-1)}, such as resulting from
39 \code{digitsBase()}.}
40 }
41 \value{
42 For \code{digitsBase()}, an object, say \code{m}, of class
43 \code{"basedInt"} which is basically a (\code{ndigits} x \code{n})
44 \code{\link{matrix}} where \code{m[,i]} corresponds to \code{x[i]},
45 \code{n <- length(x)} and \code{attr(m,"base")} is the input
46 \code{base}.
47
48 \code{as.intBase()} and the \code{\link{as.integer}} method for
49 \code{basedInt} objects return an \code{\link{integer}} vector.
50 \cr \code{bi2int()} is the low-level workhorse of \code{as.intBase()}.
51 }
52 \note{Some of these functions existed under names \code{digits} and
53 \code{digits.v} in previous versions of the \pkg{sfsmisc} package.
54 }
55 \author{Martin Maechler, Dec 4, 1991 (for S-plus; then called \code{digits.v}).}
56 \examples{
57 digitsBase(0:12, 8) #-- octal representation
58 empty.dimnames(digitsBase(0:33, 2)) # binary
59
60 ## This may be handy for just one number (and default decimal):
61 digits <- function(n, base = 10) as.vector(digitsBase(n, base = base))
62 digits(128982734) # 1 2 8 9 8 2 7 3 4
63 digits(128, base = 8) # 2 0 0
64
65 ## one way of pretty printing (base <= 10!)
66 b2ch <- function(db)
67 noquote(gsub("^0+(.{1,})$"," \\\\1", % \\\\ |--> \\ in example R core
68 apply(db, 2, paste, collapse = "")))
69 b2ch(digitsBase(0:33, 2)) #-> 0 1 10 11 100 101 ... 100001
70 b2ch(digitsBase(0:33, 4)) #-> 0 1 2 3 10 11 12 13 20 ... 200 201
71
72 ## Hexadecimal:
73 i <- c(1:20, 100:106)
74 M <- digitsBase(i, 16)
75 hexdig <- c(0:9, LETTERS[1:6])
76 cM <- hexdig[1 + M]; dim(cM) <- dim(M)
77 b2ch(cM) #-> 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 ... 6A
78
79 ## IP (Internet Protocol) numbers coding: <n>.<n>.<n>.<n> <--> longinteger
80 ip_ntoa <- function(n)
81 apply(digitsBase(n, base = 256), 2, paste, collapse=".")
82 ip_ntoa(2130706430 + (0:9))# "126.255.255.254" ... "127.0.0.7"
83 ## and the inverse:
84 ip_aton <- function(a)
85 bi2int(lapply(strsplit(a, ".", fixed=TRUE), as.integer), 256)
86
87 n <- 2130706430 + (0:9)
88 head(ip <- ip_ntoa(n))
89 head(ip_aton(ip))
90 stopifnot( n == ip_aton(ip_ntoa(n )),
91 ip == ip_ntoa(ip_aton(ip)))
92
93
94 ## Inverse of digitsBase() : as.integer method for the "basedInt" class
95 as.integer(M)
96 ## or also as.intBase() working from strings:
97 (cb <- apply(digitsBase(0:33, 4), 2, paste, collapse = ""))
98 ##-> "000" "001" ..... "200" "201"
99 all(0:33 == as.intBase(cb, base = 4))
100 }
101 \keyword{arith}
102 \keyword{utilities}