Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert between decimal and hex?

Tags:

r

decimal

hex

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
like image 630
Jian Avatar asked Aug 08 '17 16:08

Jian


People also ask

What is FFFF hex in decimal?

My book says the hexadecimal notation FFFF equals 65535 in decimal value.


1 Answers

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.

like image 111
Spätzle Avatar answered Oct 12 '22 11:10

Spätzle