Re: [R] hex format

From: Earl F. Glynn <efg_at_stowers-institute.org>
Date: Sat 09 Apr 2005 - 01:38:05 EST

"Thomas Lumley" <tlumley@u.washington.edu> wrote in message news:Pine.A41.4.61b.0504071353500.346778@homer12.u.washington.edu...
> Yes, and convertColor in R-devel does quite a few of these (XYZ
> tristimulus space; CIE Lab and Luv; sRGB, Apple RGB and roll-your-own
> RGB based on chromaticities of the primaries; and chromatic adaptation for
> changing the white point). The "colorspace" package has a more elegant
> implementation of a somewhat different set of color space computations,
> and R-devel also has hcl() for specifying colors based on hue, chroma, and
> luminance (polar coordinates in Luv space).

Thank you for this info.

I struggle to keep up with what is on CRAN and in Bioconductor, so I haven't looked at the newer packages in R-devel. Both "convertColor" and "colorspace" look interesting and I'll definitely investigate them.

I see the latest (first?) version of colorspace was introduced in January 2005 and provides functions hex, hex2RGB, readhex and writehex, and these functions seem to address my color concerns. But I was using color manipulations as an example of a need for more general capapabilities in R to do these hex conversions when needed, and without a special package.

I recently discovered R's writeBin and readBin functions. readBin, in particular, looks like an extremely powerful tool for loading external files. This might be an easier way to perform some one-time data manipulations instead of writing a separate C, Perl or Delphi program to manipulate data before bringing it into R for analysis.

readBin returns a "raw" data type that looks like an array of bytes that R displays in hex. That's where my problem begins and why I joined this "hex format" thread.

I'd love to manipulate this raw hex data, but R makes using these hex bytes quite difficult to use (or more difficult than I think it should be based on how easy it is in other languages). I might want to interpret a byte, or a string of bytes, as characters. Or, I might want to interpret pairs of bytes as 2-byte unsigned (or possibly signed) integers. Or, I might want to interpret 3-bytes at a time as RGB values (in either RGB or BGR order). Or, I might want to interpret 8-bytes at a type as a IEEE754 floating point number.

Every time someone wants to manipulate this raw data in R, as far as I can tell now, one must start from scratch and do all sorts of manipulations on these hex byte values to get characters, or integers, or doubles. And because it's not that easy (but it's not that hard either, more of an annoyance), I guess many just go back to C to get that job done there and then return to R.

Perhaps I've missed some R functions that do this conversion, but if not, I suggest some new functions that take an array of raw bytes like this, and return either a single value, or an array of values, under a given "interpretation" would be a useful addition to R for working with raw data (like flow cytometery data --
http://www.softflow.com/sf_www/fcap/Fcsformt.htm, or even TIFF files, or other forms of scientific data).

Examples of writeBin/readBin:
# Integers
> IntegerSize <- 4 # How do I get this value from R?

> i <- -2:2

> i

[1] -2 -1 0 1 2

> writeBin(i, "big.bin", endian="big")

> big <- readBin("big.bin", "raw", length(i)*IntegerSize)

> big

 [1] ff ff ff fe ff ff ff ff 00 00 00 00 00 00 00 01 00 00 00 02

> typeof(big)

[1] "raw"

> writeBin(i, "little.bin", endian="little")

> little <- readBin("little.bin", "raw", length(i)*IntegerSize)

> little

 [1] fe ff ff ff ff ff ff ff 00 00 00 00 01 00 00 00 02 00 00 00

#Doubles

> DoubleSize <- 8

> x <- 10^(-2:2)

> x

[1] 1e-02 1e-01 1e+00 1e+01 1e+02

> writeBin(x, "big.bin", endian="big")

> big <- readBin("big.bin", "raw", length(x)*DoubleSize)

> big

 [1] 3f 84 7a e1 47 ae 14 7b 3f b9 99 99 99 99 99 9a 3f f0 00 00 00 00 00 00 40 24 00 00 00 00 00 00 40 59 00 00 00 00 00 00

> writeBin(x, "platform.bin", endian=.Platform$endian)

> platform <- readBin("platform.bin", "raw", length(x)*DoubleSize)

> platform

 [1] 7b 14 ae 47 e1 7a 84 3f 9a 99 99 99 99 99 b9 3f 00 00 00 00 00 00 f0 3f 00 00 00 00 00 00 24 40 00 00 00 00 00 00 59 40

> y <- readBin("platform.bin", "double", length(x)*DoubleSize)

> y

[1] 1e-02 1e-01 1e+00 1e+01 1e+02

efg



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Apr 09 01:50:52 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:05 EST