# Re: [R] hex format

From: Earl F. Glynn <efg_at_stowers-institute.org>
Date: Fri 08 Apr 2005 - 02:58:48 EST

"Duncan Murdoch" <murdoch@stats.uwo.ca> wrote in message news:42555459.6040106@stats.uwo.ca...
> > Seems to me the conversion from hex to decimal should be system
independent
> > (and makes working with colors much more convenient). Why isn't this
system
> > independent now?
>
> Presumably because nobody thought it was important enough to make it so.
> R isn't a low level system programming language, so why should it
> treat hex specially?

1. While generally I'd agree with your statement, manipulating colors is one place the ability to convert to/from hex would be quite nice.

> rgb(1,0,0.5)
[1] "#FF0080"

rgb returns a hex string and then R makes manipulating this string somewhat difficult. One might want to use such color values to convert to a different color space, perform some sort of manipulation in that other color space, and then convert back to rgb.

2) I would think that one of R's mathematical abilities would be to provide a way to convert from any base to base 10, and from base 10 to any base. I haven't found this general math tool yet in R. Working with base-16 (or even base 2 sometimes) could be done with such a general math tool.

3) While I may be in a minority, I would even consider exporting IEEE floating-point numbers in hex form as a way to avoid any additional conversion losses converting to/from decimal.

4) Why not make working with "raw" data a little easier? readbin shows hex values but they are not easy to work with inside of R.

> IntegerSize <- 4 # How do I get this value from R?
> i <- -2:2
> i

[1] -2 -1 0 1 2
> length(i)

[1] 5
> object.size(i)

[1] 52
>
> 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
>
> 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
>