Boom 2k1
2006-May-08 18:44 UTC
[R] Calling C++ code fom R --How to export C++ "unsigned" integer to R?
Hello all, Is there a way to export C++ "unsigned" integer to R? (I am trying to parse files in "BPMAP" format, and some variables are of type unsigned int (first declared in C++) ). I know that to export signed integer to R, I can do the following in C++: int Some_INTEGER = 5; int *p_myInt; SEXP myInt; PROTECT(myInt=NEW_INTEGER(1)); myInt[0] = Some_INTEGER; UNPROTECT(1); return myInt; However, it appears that myInt is a signed integer. I have looked over Rdefines.h and it does not look like there is a definition for NEW_UNSIGNED_INTEGER ! How would I export an unsigned integer to R? obviously this won't work: unsigned int Some_INTEGER = 5; unsigned int *p_myInt; SEXP myInt; PROTECT(myInt=NEW_INTEGER(1)); myInt[0] = Some_INTEGER; UNPROTECT(1); return myInt; This would give me a casting error! How should I get around with this? Thanks! Charles Cheung
Duncan Murdoch
2006-May-08 18:56 UTC
[R] Calling C++ code fom R --How to export C++ "unsigned" integer to R?
On 5/8/2006 2:44 PM, Boom 2k1 wrote:> Hello all, > > Is there a way to export C++ "unsigned" integer to R? (I am trying to parse > files in "BPMAP" format, and some variables are of type unsigned int (first > declared in C++) ). > > I know that to export signed integer to R, > I can do the following in C++: > > int Some_INTEGER = 5; > > int *p_myInt; > SEXP myInt; > PROTECT(myInt=NEW_INTEGER(1)); > > myInt[0] = Some_INTEGER; > > UNPROTECT(1); > > return myInt; > > > However, it appears that myInt is a signed integer. > > I have looked over Rdefines.h and it does not look like there is a > definition for NEW_UNSIGNED_INTEGER !R has no type that corresponds to an unsigned integer. You should convert these to floating point if you want to keep the values. (The double precision floating point format that R uses stores all 32 bit unsigned integers exactly. If your compiler defines int to be 64 bits, you're in trouble, because there is no way to represent all unsigned 64 bit integers in R.) By the way, generally more technical questions like interfacing to C++ are better in the R-devel list. Duncan Murdoch