简体   繁体   English

如何在R中转换64位十六进制字符串?

[英]How do I convert 64-bit hexadecimal strings in R?

How do I convert 64-bit hexadecimal strings in R? 如何在R中转换64位十六进制字符串?

> library(int64)
> as.int64("7f2d36a2a000")
[1] NA
Warning message:
In as.int64("7f2d36a2a000") : NAs introduced
> as.int64("0x7f2d36a2a000")
[1] NA
Warning message:
In as.int64("0x7f2d36a2a000") : NAs introduced

Here's a way using bit64 . 这是使用bit64的一种方式。

library(bit64)
str <- "7f2d36a2a000"
as.integer64(as.numeric(paste0("0x",str)))
# integer64
[1] 139832166883328

For a number that large, you'll need to load a package that supports representations of arbitrarily large numbers. 对于数字很大的数字,您需要加载一个支持任意大数字表示的包。 Rmpfr is one example: Rmpfr就是一个例子:

library(Rmpfr)

## Check that it works as expected on smaller numbers:
strtoi("ff", base=16)
# [1] 255
mpfr("ff", base=16)
# 1 'mpfr' number of precision  8   bits 
# [1] 255
as.integer(mpfr("ff", base=16)
# [1] 255

## Then apply it with (more) confidence to larger numbers: 
mpfr("7f2d36a2a000", base=16)
# 1 'mpfr' number of precision  48   bits 
# [1] 139832166883328
mpfr("7f2d36a2a0007f2d36a2a0007f2d36a2a0007f2d36a2a000", base=16)
# 1 'mpfr' number of precision  192   bits 
# [1] 3118361524223520784583964884878580812558070356334996529152

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM