Title: | Convert values to/from raw vectors |
---|---|
Description: | Functions to easily convert data to binary formats other programs/machines can understand. |
Authors: | Josh Ulrich |
Maintainer: | Josh Ulrich <[email protected]> |
License: | GPL-3 |
Version: | 0.1-1 |
Built: | 2024-11-18 05:23:36 UTC |
Source: | https://github.com/joshuaulrich/pack |
pack
allows R programmers to easily put their data into
binary formats that other programs / machines can understand.
Package: | pack |
Type: | Package |
Version: | 0.1-1 |
Date: | 2008-08-22 |
License: | GPL-3 |
LazyLoad: | yes |
Author: Josh Ulrich Maintainer: Josh Ulrich <[email protected]>
http://perldoc.perl.org/functions/pack.html
Convert numeric values to a raw vector.
numToRaw(x, nBytes = 1)
numToRaw(x, nBytes = 1)
x |
A number to be converted |
nBytes |
The number of bytes to use |
A raw vector containing the bytes representing x
.
Josh Ulrich
# Will be left null padded (x <- numToRaw(421,4)) rawToNum(x,2) rawToNum(x,4)
# Will be left null padded (x <- numToRaw(421,4)) rawToNum(x,2) rawToNum(x,4)
Combine values into a raw vector according to the values in template
.
pack(template, ...)
pack(template, ...)
template |
A string, see 'Details' |
... |
Values/objects to be packed into a raw vector |
Currently supported template
values are:
'a' - A null padded string
'A' - A space padded string
'b' - An ascending bit order binary vector, (must be a multiple of 8 long)
'B' - An descending bit order binary vector, (must be a multiple of 8 long)
'C' - An unsigned char (8-bit byte/octet) value
'v' - An unsigned short (16-bit) in "VAX" (little-endian) order
'V' - An unsigned long (32-bit) in "VAX" (little-endian) order
'x' - A null byte
Both 'a' and 'A' may be followed by a repeat value. A repeat value of '*'
will cause the remainder of the bytes in values
to be placed in
the last element.
'/' allows packing and unpacking of a sequence of values where the packed
structure contains a packed item count followed by the packed items themselves.
If template
requires more arguments to pack
than actually given,
pack
pads with null bytes. If template
requires fewer
arguments to pack
than actually given, extra arguments are ignored.
A raw vector following the elements in template
.
Josh Ulrich
http://perldoc.perl.org/functions/pack.html
(x <- pack('A4 C v A8 V', 'pack', 2, 8, 'sequence', 68098)) (u1 <- unpack('A4 C H*', x)) (u2 <- unpack('v/A V', u1[[3]]))
(x <- pack('A4 C v A8 V', 'pack', 2, 8, 'sequence', 68098)) (u1 <- unpack('A4 C H*', x)) (u2 <- unpack('v/A V', u1[[3]]))
Convert raw values to numeric.
rawToNum(x, nBytes = 1)
rawToNum(x, nBytes = 1)
x |
A raw vector to be converted |
nBytes |
The number of bytes to use |
A numeric value containing the bytes in x
.
Josh Ulrich
# Will be left null padded (x <- numToRaw(421,4)) rawToNum(x,2) rawToNum(x,4)
# Will be left null padded (x <- numToRaw(421,4)) rawToNum(x,2) rawToNum(x,4)
Break a raw vector into chunks according to the values in template
.
unpack(template, ...)
unpack(template, ...)
template |
A string, see 'Details' |
... |
Raw vector(s) to be unpacked |
Currently supported template
values are:
'a' - A null padded string (as of R-2.8.0, strings cannot contain embedded nulls)
'A' - A space padded string
'b' - An ascending bit order binary vector, (must be a multiple of 8 long)
'B' - An descending bit order binary vector, (must be a multiple of 8 long)
'C' - An unsigned char (8-bit byte/octet) value
'v' - An unsigned short (16-bit) in "VAX" (little-endian) order
'V' - An unsigned long (32-bit) in "VAX" (little-endian) order
'f' - A single-precision float
'd' - A double-precision float
'x' - Skip next byte, and push nothing onto return value for it
'H' - A raw byte
Values 'a', 'A', and 'H' may be followed by a repeat value. A repeat value
of '*' will cause the remainder of the bytes in values
to be placed
in the last element.
'/' allows packing and unpacking of a sequence of values where the packed
structure contains a packed item count followed by the packed items themselves.
If there are more template
values or if the repeat count of a field
or a group is larger than what the remainder ... allows, unpack
returns NULL
. If ... is longer than what is described by
template
, the rest is ignored.
A list with an element for each value in template
.
When unpacking, 'A' strips trailing whitespace and nulls and 'a' returns data verbatim (but with embedded nulls removed, since strings cannot contain embedded nulls as of R-2.8.0).
Josh Ulrich
http://perldoc.perl.org/functions/unpack.html
(x <- pack('A4 C v A8 V', 'pack', 2, 8, 'sequence', 68098)) (u1 <- unpack('A4 C H*', x)) (u2 <- unpack('v/A V', u1[[3]]))
(x <- pack('A4 C v A8 V', 'pack', 2, 8, 'sequence', 68098)) (u1 <- unpack('A4 C H*', x)) (u2 <- unpack('v/A V', u1[[3]]))