7

I am running the code below in R, and it doesn't return the original decimal number.

as.hexmode(-8192)
"ffffe000"

strtoi(c("ffffe000"))
NA
jay.sf
  • 60,139
  • 8
  • 53
  • 110
Jian
  • 365
  • 1
  • 6
  • 19

1 Answers1

4

The strtoi function takes a string as an input (see docs). However, the as.hexmode function does not return an integer but rather a hexadecimal representation of the input, which is not a string (it is a type named hexmode AFAIK).

The proper solution, as suggested by the R documentation, is using as.integer to obtain your original input:

> strtoi(as.hexmode(-8192),16)
[1] NA
> as.integer(as.hexmode(-8192))
[1] -8192

It remains unclear to me whether the problem is with using a negative input. I can only suppose strtoi handles only unsigned ints.

Spätzle
  • 709
  • 10
  • 20