Re: [R] hex format

2005-04-07 Thread Prof Brian Ripley
On Thu, 7 Apr 2005, Steve Vejcik wrote:
Hello world:
Has anyone used hex notation within R to represents integers?
That's a spectacularly vague question.  Short answer: yes.
as.numeric("0x1AF0")
[1] 6896
(which BTW is system-dependent, but one person used it as you asked).
PLEASE read the posting guide and try for a `smarter' question.
--
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595
__
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


Re: [R] hex format

2005-04-07 Thread Steve Vejcik
Thanks for your advice.  Unfortunately, your answers are inconsistent:
as.numeric("0x1AF0") returns a decimal value for a hex string. I'd like
to do the opposite-use hex notation to represent a decimal.
e.g.
x<-0x000A
y<-0x0001
x+y=0x00B
 
 Cheers.

On Thu, 2005-04-07 at 08:45, Prof Brian Ripley wrote:
> On Thu, 7 Apr 2005, Steve Vejcik wrote:
> 
> > Hello world:
> > Has anyone used hex notation within R to represents integers?
> 
> That's a spectacularly vague question.  Short answer: yes.
> 
> > as.numeric("0x1AF0")
> [1] 6896
> 
> (which BTW is system-dependent, but one person used it as you asked).
> 
> PLEASE read the posting guide and try for a `smarter' question.

__
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


Re: [R] hex format

2005-04-07 Thread Duncan Murdoch
Steve Vejcik wrote:
Thanks for your advice.  Unfortunately, your answers are inconsistent:
as.numeric("0x1AF0") returns a decimal value for a hex string. I'd like
to do the opposite-use hex notation to represent a decimal.
e.g.
x<-0x000A
y<-0x0001
x+y=0x00B
R doesn't use decimal or hex values internally, it stores values in the 
native format (which is 64 bit floating point or 32 bit binary integers 
on most platforms).

You're talking about string conversions on input and output, which is a 
different issue.  R doesn't support C-style hex notation on input 
(though you can use "as.numeric" on input, as Brian said).

If you want an integer vector to always display in hex, assign a class 
to it and define a print method.  I don't think there's a standard 
library function to display in hex, but there are probably packages to 
do so.

Duncan Murdoch
__
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


Re: [R] hex format

2005-04-07 Thread Earl F. Glynn
"Prof Brian Ripley" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> On Thu, 7 Apr 2005, Steve Vejcik wrote:
> > Has anyone used hex notation within R to represents integers?

>  Short answer: yes.
>
> > as.numeric("0x1AF0")
> [1] 6896
>
> (which BTW is system-dependent, but one person used it as you asked).

I see this works fine with R 2.0.0 on a Linux platform, but doesn't work at
all under R 2.0.1 on Windows.

> as.numeric("0x1AF0")
[1] NA
Warning message:
NAs introduced by coercion

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?

The "prefix" on hex numbers is somewhat language dependent ("0x" or $)
perhaps but I didn't think this conversion should be system dependent.

I don't remember where I got this, but this hex2dec works under both Linux
and Windows (and doesn't need the "0x" prefix).

hex2dec <- function(hexadecimal)
{
  hexdigits <- c(0:9, LETTERS[1:6])
  hexadecimal <- toupper(hexadecimal)# treat upper/lower case the same
  decimal <- rep(0, length(hexadecimal))
  for (i in 1:length(hexadecimal))
  {
digits <- match(strsplit(hexadecimal[i],"")[[1]], hexdigits) - 1
decimal[i] <- sum(digits * 16^((length(digits)-1):0))
  }
  return(decimal)
}

Example:
> hex2dec(c("1AF0", ""))
[1]  6896 65535

"" can be interpreted as 65535 as unsigned and -1 as signed on the same
system depending on context.  This isn't system dependent, but rather
context dependent.

I suggest "as.numeric" should perform the unsigned conversion on all
systems.  What am I missing?

efg
--
Earl F. Glynn
Scientific Programmer
Bioinformatics Department
Stowers Institute for Medical Research

__
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


Re: [R] hex format

2005-04-07 Thread Duncan Murdoch
Earl F. Glynn wrote:
"Prof Brian Ripley" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
On Thu, 7 Apr 2005, Steve Vejcik wrote:
Has anyone used hex notation within R to represents integers?

Short answer: yes.

as.numeric("0x1AF0")
[1] 6896
(which BTW is system-dependent, but one person used it as you asked).

I see this works fine with R 2.0.0 on a Linux platform, but doesn't work at
all under R 2.0.1 on Windows.

as.numeric("0x1AF0")
[1] NA
Warning message:
NAs introduced by coercion
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?

Duncan Murdoch
__
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


Re: [R] hex format

2005-04-07 Thread Peter Wolf
Steve Vejcik wrote:
Thanks for your advice.  Unfortunately, your answers are inconsistent:
as.numeric("0x1AF0") returns a decimal value for a hex string. I'd like
to do the opposite-use hex notation to represent a decimal.
e.g.
   x<-0x000A
   y<-0x0001
   x+y=0x00B

Cheers.

you can use chcode() to define hex.to.dec(), dec.to.hex() and sum.hex()
to operate with hex numbers.
Peter Wolf
--
<>=
chcode <- function(b, base.in=2, base.out=10, digits="0123456789ABCDEF"){
  # change of number systems, pwolf 10/02
  # e.g.: from 2 2 2 2 ...  ->  16 16 16 ...
  digits<-substring(digits,1:nchar(digits),1:nchar(digits))
  if(length(base.in)==1) base.in <- rep(base.in, max(nchar(b)-1))
  if(is.numeric(b)) b <- as.character(as.integer(b))
  b.num <- lapply(strsplit(b,""), function(x) match(x,digits)-1  )
  result <- lapply(b.num, function(x){
   cumprod(rev(c(base.in,1))[ 1:length(x) ] ) %*% rev(x)
} )
  number<-unlist(result)
  cat("decimal representation:",number,"\n")
  if(length(base.out)==1){
 base.out<-rep(base.out,1+ceiling(log( max(number), base=base.out ) ) )
  }
  n.base <- length(base.out); result <- NULL
  for(i in n.base:1){
result <- rbind(number %% base.out[i], result)
number <- floor(number/base.out[i])
  }
  result[]<-digits[result+1]
  apply(result, 2, paste, collapse="")
}
@
<>=
hex.to.dec<-function(x) as.numeric(chcode(x, base.in=16, base.out=10))
dec.to.hex<-function(x) chcode(x, base.in=10, base.out=16)
sum.hex<-function(x,y) dec.to.hex(hex.to.dec(x) + hex.to.dec(y))
@
quick test:
<>=
a<-dec.to.hex(10); print(a)
b<-dec.to.hex(3);print(b)
@
output-start
decimal representation: 10
[1] "0A"
decimal representation: 3
[1] "03"
output-end
@
<>=
sum.hex(a,b)
@
output-start
decimal representation: 10
decimal representation: 3
decimal representation: 13
Thu Apr  7 17:31:42 2005
[1] "0D"
output-end
 

__
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


Re: [R] hex format

2005-04-07 Thread Prof Brian Ripley
On Thu, 7 Apr 2005, Steve Vejcik wrote:
Thanks for your advice.  Unfortunately, your answers are inconsistent:
as.numeric("0x1AF0") returns a decimal value for a hex string. I'd like
You don't understand how R works:
x <- as.numeric("0x1AF0")
produces an number, not its decimal representation.  A number is a number 
is a number irrepsective of the the base of its character representation.

to dothe opposite-use hex notation to represent a decimal.
e.g.
   x<-0x000A
   y<-0x0001
   x+y=0x00B
Cheers.
On Thu, 2005-04-07 at 08:45, Prof Brian Ripley wrote:
On Thu, 7 Apr 2005, Steve Vejcik wrote:
Hello world:
Has anyone used hex notation within R to represents integers?
That's a spectacularly vague question.  Short answer: yes.
as.numeric("0x1AF0")
[1] 6896
(which BTW is system-dependent, but one person used it as you asked).
PLEASE read the posting guide and try for a `smarter' question.

--
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595
__
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


Re: [R] hex format

2005-04-07 Thread Prof Brian Ripley
On Thu, 7 Apr 2005, Duncan Murdoch wrote:
[...]
If you want an integer vector to always display in hex, assign a class to it 
and define a print method.  I don't think there's a standard library function 
to display in hex, but there are probably packages to do so.
In R 2.1.0-to-be
x <- as.numeric("0x00B")  # this is platform-specific
x
[1] 11
sprintf("0x%X", as.integer(x))  # this is not
[1] "0xB"
--
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595
__
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


Re: [R] hex format

2005-04-07 Thread Earl F. Glynn
"Duncan Murdoch" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> > 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
>

5) Does R have a hex consistency problem?  The color values start with a "#"
for hex, but the as.numeric("#FF0080") isn't allowed?


Thanks for your time.

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


Re: [R] hex format

2005-04-07 Thread Steve Vejcik
On Thu, 2005-04-07 at 11:06, Prof Brian Ripley wrote:
> On Thu, 7 Apr 2005, Steve Vejcik wrote:
> 
> > Thanks for your advice.  Unfortunately, your answers are inconsistent:
> > as.numeric("0x1AF0") returns a decimal value for a hex string. I'd like
> 
> You don't understand how R works:
> 
> x <- as.numeric("0x1AF0")
> 
> produces an number, not its decimal representation.  A number is a number 
> is a number irrepsective of the the base of its character representation.
> 
"as.numeric("0x1AF0") returns a decimal value for a hex string.
If you prefer, substitute the word "shows" for "returns".

> > to dothe opposite-use hex notation to represent a decimal.
> > e.g.
> >x<-0x000A
> >y<-0x0001
> >x+y=0x00B
> >
> > Cheers.
> >
> > On Thu, 2005-04-07 at 08:45, Prof Brian Ripley wrote:
> >> On Thu, 7 Apr 2005, Steve Vejcik wrote:
> >>
> >>> Hello world:
> >>>   Has anyone used hex notation within R to represents integers?
> >>
> >> That's a spectacularly vague question.  Short answer: yes.
> >>
> >>> as.numeric("0x1AF0")
> >> [1] 6896
> >>
> >> (which BTW is system-dependent, but one person used it as you asked).
> >>
> >> PLEASE read the posting guide and try for a `smarter' question.
> >
> >

__
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


RE: [R] hex format

2005-04-07 Thread Liaw, Andy
> From: Steve Vejcik
> 
> On Thu, 2005-04-07 at 11:06, Prof Brian Ripley wrote:
> > On Thu, 7 Apr 2005, Steve Vejcik wrote:
> > 
> > > Thanks for your advice.  Unfortunately, your answers are 
> inconsistent:
> > > as.numeric("0x1AF0") returns a decimal value for a hex 
> string. I'd like
> > 
> > You don't understand how R works:
> > 
> > x <- as.numeric("0x1AF0")
> > 
> > produces an number, not its decimal representation.  A 
> number is a number 
> > is a number irrepsective of the the base of its character 
> representation.
> > 
> "as.numeric("0x1AF0") returns a decimal value for a hex string.
> If you prefer, substitute the word "shows" for "returns".

You don't seem to get the point.  as.numeric() is a function that _returns_
a _value_.  How you want that _value_ to be _shown_ is a different matter.
Would you substitute `I gave the money to the cashier' with `I showed the
money to the cashier'?

Andy

 
> > > to dothe opposite-use hex notation to represent a decimal.
> > > e.g.
> > >x<-0x000A
> > >y<-0x0001
> > >x+y=0x00B
> > >
> > > Cheers.
> > >
> > > On Thu, 2005-04-07 at 08:45, Prof Brian Ripley wrote:
> > >> On Thu, 7 Apr 2005, Steve Vejcik wrote:
> > >>
> > >>> Hello world:
> > >>> Has anyone used hex notation within R to 
> represents integers?
> > >>
> > >> That's a spectacularly vague question.  Short answer: yes.
> > >>
> > >>> as.numeric("0x1AF0")
> > >> [1] 6896
> > >>
> > >> (which BTW is system-dependent, but one person used it 
> as you asked).
> > >>
> > >> PLEASE read the posting guide and try for a `smarter' question.
> > >
> > >
> 
> __
> 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
> 
> 
>

__
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


RE: [R] hex format

2005-04-07 Thread Steve Vejcik
I understand that point.

Again:

I would like to have numbers represented
to me in hexidecimal format, not decimal format.
This was my original query and I think it's clear.
Let me try another variation:
I would like R to recognize that I am using
hexadecimal notation when I type a number at the
keyboard.
I would like R to have the ability to show me an
integer expressed in hexadecimal format.

On Thu, 2005-04-07 at 12:12, Liaw, Andy wrote:
> > From: Steve Vejcik
> > 
> > On Thu, 2005-04-07 at 11:06, Prof Brian Ripley wrote:
> > > On Thu, 7 Apr 2005, Steve Vejcik wrote:
> > > 
> > > > Thanks for your advice.  Unfortunately, your answers are 
> > inconsistent:
> > > > as.numeric("0x1AF0") returns a decimal value for a hex 
> > string. I'd like
> > > 
> > > You don't understand how R works:
> > > 
> > > x <- as.numeric("0x1AF0")
> > > 
> > > produces an number, not its decimal representation.  A 
> > number is a number 
> > > is a number irrepsective of the the base of its character 
> > representation.
> > > 
> > "as.numeric("0x1AF0") returns a decimal value for a hex string.
> > If you prefer, substitute the word "shows" for "returns".
> 
> You don't seem to get the point.  as.numeric() is a function that _returns_
> a _value_.  How you want that _value_ to be _shown_ is a different matter.
> Would you substitute `I gave the money to the cashier' with `I showed the
> money to the cashier'?
> 
> Andy
> 
>  
> > > > to dothe opposite-use hex notation to represent a decimal.
> > > > e.g.
> > > >x<-0x000A
> > > >y<-0x0001
> > > >x+y=0x00B
> > > >
> > > > Cheers.
> > > >
> > > > On Thu, 2005-04-07 at 08:45, Prof Brian Ripley wrote:
> > > >> On Thu, 7 Apr 2005, Steve Vejcik wrote:
> > > >>
> > > >>> Hello world:
> > > >>>   Has anyone used hex notation within R to 
> > represents integers?
> > > >>
> > > >> That's a spectacularly vague question.  Short answer: yes.
> > > >>
> > > >>> as.numeric("0x1AF0")
> > > >> [1] 6896
> > > >>
> > > >> (which BTW is system-dependent, but one person used it 
> > as you asked).
> > > >>
> > > >> PLEASE read the posting guide and try for a `smarter' question.
> > > >
> > > >
> > 
> > __
> > 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
> > 
> > 
> > 
> 
> 
> 
> --
> Notice:  This e-mail message, together with any attachment...{{dropped}}

__
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


Re: [R] hex format

2005-04-07 Thread Thomas Lumley
On Thu, 7 Apr 2005, Earl F. Glynn wrote:
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.
The convertColor function in R 2.1.0 provides colorspace conversion, 
including "hex".

5) Does R have a hex consistency problem?  The color values start with a "#"
for hex, but the as.numeric("#FF0080") isn't allowed?
#ff0080 isn't a number, it's a colour (or perhaps a color). If it were 
converted to numeric form it would be a vector of three numbers, and which 
three numbers would depend on the coordinate system used for colour space. 
For example, R already provides both hsv() and rgb() to create colours 
from vectors of three numbers, but the correspondence is different in each 
case.

-thomas
__
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


Re: [R] hex format

2005-04-07 Thread Jan T. Kim
On Thu, Apr 07, 2005 at 11:58:48AM -0500, Earl F. Glynn wrote:
> "Duncan Murdoch" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> > > 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.

I'd like to second this opinion. It just occasionally happens that data are
available in some variant of hex format, and I've had the impression that
getting such data into R is a bit less convenient than it could be.

> 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.

In fact, the ANSI C function strtol already provides conversion to any
base between 2 and 36, so R's mathematical capabilities don't even need
to be invoked here.

An R function strtol(x, base), x being a character variable and base an
integer between 2 and 36, would probably add a bit of convenience. I've
never programmed that, though -- seems that I'm one of those to whom this
hasn't been important enough.

If it is done some day, I'd favour the strtol function over having as.numeric
interpret the (rather C-ish) 0x prefix. I wasn't aware that this currently
works on some platforms (and I'm glad it doesn't interpret the 0 prefix for
octal, as C does, making 007 legal and 008 not.  ;-)  )

Best regards, Jan
-- 
 +- Jan T. Kim ---+
 |*NEW*email: [EMAIL PROTECTED]   |
 |*NEW*WWW:   http://www.cmp.uea.ac.uk/people/jtk |
 *-=<  hierarchical systems are for files, not for humans  >=-*

__
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


Re: [R] hex format

2005-04-07 Thread Earl F. Glynn
"Thomas Lumley" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> The convertColor function in R 2.1.0 provides colorspace conversion,
> including "hex".

> #ff0080 isn't a number, it's a colour (or perhaps a color). If it were
> converted to numeric form it would be a vector of three numbers, and which
> three numbers would depend on the coordinate system used for colour space.

Colo(u)rs and numbers are interchangeable to me.  When you look at a
picture, don't you "see" numbers?

Maybe you don't see a number here, but I do. #ff0080 is interpreted in some
(non-R) contexts as a single number.  In many contexts, including HTML,
colors are represented as three bytes in hex with this notation and the "#"
means "hexadecimal".  The RGB color componets can be discerned quite easily:
hex FF is decimal 255 (red), hex 00 is decimal 0 (green), hex 80 is decimal
128 (blue).  Some programs, e.g., Dreamweaver, allow specification of colors
in this hex 3-byte form directly.  The "16 million" colors you seen on a
"true color" display are from the 256*256*256 (or in hex FF*FF*FF) possible
RGB triples.

> For example, R already provides both hsv() and rgb() to create colours
> from vectors of three numbers, but the correspondence is different in each
> case.

Sorry if some consider this off topic:
HSV as a color space is really only liked by computer scientists.  Image
processing and color engineers rarely if ever use HSV.

There are MANY other color spaces and computations possible (see "color
spaces" or "color conversions" or other color topics on  this page
http://www.efg2.com/Lab/Library/Color/Science.htm).  Most of these color
manipulations in R are not easy because the very first step, converting
colors, I mean numbers , like #ff0080 to the red, green components is
hindered because one must reinvent the wheel of hex-to-decimal conversion.

Perhaps R will someday introduce a "pixel" type that would encapsulate the
three color components (for color images at least).  A matrix of pixels
could easily be made into an image.  Some color computations such a Maxwell
Triangle, or a CIE Chromaticity Chart (sorry the links are currently broken,
but the image can be seen on this Chinese translation page)
http://bluemoon.myrice.com/efg/color/chromaticity.htm in R is more difficult
than it should be because of how R is designed now.  Many image processing
statistical problems could be tackled directly in R if there were an easier
way to manipulate pixels and images.

But the hex manipulations I'm advocating could be used for variety of other
purposes.  E.g, I must periodically deal with a binary data stream of flow
cytometery data -- part ASCII, part binary.  Reading this stream directly
from R would be nice and is almost doable.  Working with raw data and
understanding  exactly what you've got would be facilitated by better
conversion capabilities within R.

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


Re: [R] hex format

2005-04-07 Thread Thomas Lumley
On Thu, 7 Apr 2005, Earl F. Glynn wrote:
For example, R already provides both hsv() and rgb() to create colours
from vectors of three numbers, but the correspondence is different in each
case.
Sorry if some consider this off topic:
HSV as a color space is really only liked by computer scientists.  Image
processing and color engineers rarely if ever use HSV.
There are MANY other color spaces and computations possible (see "color
spaces" or "color conversions" or other color topics on  this page
http://www.efg2.com/Lab/Library/Color/Science.htm).  Most of these color
manipulations in R are not easy because the very first step, converting
colors, I mean numbers , like #ff0080 to the red, green components is
hindered because one must reinvent the wheel of hex-to-decimal conversion.
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).

Basing R graphics on these (and so making them colours rather than just 
data about colours) requires a further step of considering the 
characteristics of the output device. This might be as simple as declaring 
R's output to be sRGB or as complicated as worrying about ICC profiles.

-thomas
__
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


Re: [R] hex format

2005-04-07 Thread David Forrest
On Thu, 7 Apr 2005, Earl F. Glynn wrote:

...
> picture, don't you "see" numbers?
>
> Maybe you don't see a number here, but I do. #ff0080 is interpreted in some
> (non-R) contexts as a single number.  In many contexts, including HTML,


> colors are represented as three bytes in hex with this notation and the "#"
> means "hexadecimal".  The RGB color componets can be discerned quite easily:
> hex FF is decimal 255 (red), hex 00 is decimal 0 (green), hex 80 is decimal
> 128 (blue).  Some programs, e.g., Dreamweaver, allow specification of colors
> in this hex 3-byte form directly.  The "16 million" colors you seen on a
> "true color" display are from the 256*256*256 (or in hex FF*FF*FF) possible
> RGB triples.
>
> > For example, R already provides both hsv() and rgb() to create colours
> > from vectors of three numbers, but the correspondence is different in each
> > case.
>
> Sorry if some consider this off topic:
> HSV as a color space is really only liked by computer scientists.  Image
> processing and color engineers rarely if ever use HSV.
>
> There are MANY other color spaces and computations possible (see "color
> spaces" or "color conversions" or other color topics on  this page
> http://www.efg2.com/Lab/Library/Color/Science.htm).  Most of these color
> manipulations in R are not easy because the very first step, converting
> colors, I mean numbers , like #ff0080 to the red, green components is
> hindered because one must reinvent the wheel of hex-to-decimal conversion.

I think R has the hex to decimal OK, but might be lacking in the decimal
to hex case

zz<-function(x){
x<-as.numeric(sub("#",'0x',x));
c(x%/%256^2,
  x%/%256%%256,
  x%%256) }

> zz('#0f0e0d')
[1] 15 14 13
> zz('#ff0080')
[1] 255   0 128

If you already have the 3 byte triplet in read in as a binary, the same
integer arithmetic does the extraction.

> Perhaps R will someday introduce a "pixel" type that would encapsulate the
> three color components (for color images at least).  A matrix of pixels
> could easily be made into an image.  Some color computations such a Maxwell
> Triangle, or a CIE Chromaticity Chart (sorry the links are currently broken,
> but the image can be seen on this Chinese translation page)
> http://bluemoon.myrice.com/efg/color/chromaticity.htm in R is more difficult
> than it should be because of how R is designed now.  Many image processing
> statistical problems could be tackled directly in R if there were an easier
> way to manipulate pixels and images.
>
> But the hex manipulations I'm advocating could be used for variety of other
> purposes.  E.g, I must periodically deal with a binary data stream of flow
> cytometery data -- part ASCII, part binary.  Reading this stream directly
> from R would be nice and is almost doable.  Working with raw data and
> understanding  exactly what you've got would be facilitated by better
> conversion capabilities within R.

I'm still not sure what you mean by hex manipulations.

R has string manipulations, hex-to-number manipulations,
binary-file-to-number manipulations, mixed file to number manipulations,
and number to number manipulations.

What I think you are asking for is /displaying/ numbers.

Since R's sprintf() doesn't support the %x, (or %o, or %u) formats, I'm
not sure how to use R to translate the number 257 into #000101

zzinv<-function(x){}
# such that:

> zzinv(257) #or zzinv(c(0,1,1))
"#000101"

Is zzinv() the operation you need?

Dave
-- 
 Dr. David Forrest
 [EMAIL PROTECTED](804)684-7900w
 [EMAIL PROTECTED] (804)642-0662h
   http://maplepark.com/~drf5n/

__
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


Re: [R] hex format

2005-04-07 Thread Martin Maechler
> "David" == David Forrest <[EMAIL PROTECTED]>
> on Thu, 7 Apr 2005 16:19:33 -0500 (CDT) writes:





David> I think R has the hex to decimal OK, but might be
David> lacking in the decimal to hex case

David> zz<-function(x){ x<-as.numeric(sub("#",'0x',x));
David> c(x%/%256^2, x%/%256%%256, x%%256) }

>> zz('#0f0e0d')
David> [1] 15 14 13
>> zz('#ff0080')
David> [1] 255 0 128

I think you have overlooked  the  col2rgb() function
which does this (and more).

David> If you already have the 3 byte triplet in read in as
David> a binary, the same integer arithmetic does the
David> extraction.

__
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


Re: [R] hex format

2005-04-08 Thread Earl F. Glynn
"Thomas Lumley" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> 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


Re: [R] hex format

2005-04-08 Thread Duncan Murdoch
Earl F. Glynn wrote:
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.
 

That's only one possibility.  readBin can return a number of different 
types.

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. 

A simple solution to this would be to implement a "raw connection", that 
takes a raw variable and lets you read it using readBin.

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.
 

Currently you can do it by writing the raw data out to a temporary file 
and reading it back in.  It would be nice to allow this to happen 
without the temporary file.

Duncan Murdoch
__
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