【问题标题】:How to convert between decimal and hex?如何在十进制和十六进制之间转换?
【发布时间】:2018-01-16 07:55:50
【问题描述】:

我在 R 中运行下面的代码,它不返回原始的十进制数。

as.hexmode(-8192)
"ffffe000"

strtoi(c("ffffe000"))
NA

【问题讨论】:

    标签: r hex decimal


    【解决方案1】:

    strtoi 函数将字符串作为输入(请参阅docs)。但是,as.hexmode 函数不返回整数,而是返回输入的十六进制表示,它不是字符串(它是一种名为 hexmodeAFAIK 的类型)。

    正如R documentation 所建议的,正确的解决方案是使用as.integer 来获取您的原始输入:

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

    我仍然不清楚问题是否与使用负输入有关。我只能假设strtoi 只处理无符号整数。

    【讨论】:

      猜你喜欢
      • 2010-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多