【问题标题】:Using as.hexmode in R to handle a double在 R 中使用 as.hexmode 处理双精度
【发布时间】:2014-07-21 19:40:14
【问题描述】:

我在 R 中遇到了 as.hexmode 的间歇性问题。

根据上一个问题的好建议,我现在的代码如下所示:

WriteOutRW<- function(page, offset, lengthI) {
#  writePoint <- bitwShiftL(page, 12)
  writePoint <- page * 2^12 + offset
  localCount<-0
  instructions <- 0
  while(localCount < lengthI$length) {
    if (runif(1, 0, 1) > 0.9) {
      cat("<modify")
    } else {
      cat("<load")
    }
  cat(" address=\"")
  cat(as.character(as.hexmode(writePoint)))
  .
  .
  .

这会产生这样的输出......

<instruction address="4d0bff" size="3" />
<instruction address="4d0c02" size="2" />
<load address="4e36e0" size="c" />
<load address="4e36ec" size="1" />
<load address="4e36ed" size="1" />
<load address="4e36ee" size="2" />
<instruction address="4d0328" size="2" />
<instruction address="4d032a" size="2" />
<modify address="4df2dd" size="1" />
<load address="4df2de" size="1" />
<modify address="4df2df" size="2" />
<instruction address="4d0204" size="2" />
<instruction address="4d0206" size="1" />
<instruction address="4d0207" size="1" />
<load address="Error in if (is.double(x) && (x == as.integer(x))) x <- as.integer(x) : 
  missing value where TRUE/FALSE needed
Calls: WriteOutRW -> cat -> as.hexmode
In addition: Warning message:
In as.hexmode(writePoint) : NAs introduced by coercion
Execution halted

instruction 元素是由一个非常相似的函数产生的——尽管address 永远不可能是双精度的——而且它永远不会失败。我的假设是 WriteOutRW 函数在 writePoint 是双精度时失败。但是as.hexmode 应该处理双打,不是吗?

更新为了清楚起见,我在这里尝试处理整数(整数和双精度类型),并且数字打印为以 10 为底的小数没有问题,但我真的想要十六进制输出。

【问题讨论】:

  • 你的意思是像as.hexmode(3.14)吗?因为那会报错。
  • as.hexmode 和/或?as.hexmode 有答案..“整数(类型为'integer'或'double')和元素仅包含0-9、a-f、 A-F(或不适用)”
  • 不,绝对不是 3.14 类型 - 而且我不明白你的评论 - 我正在尝试处理该类型的双精度(我确实阅读了帮助文件,这就是我感到困惑的原因)
  • 打印所有as.integer(writePoint)s 然后
  • 你能在这里给出一个产生问题的示例值吗?我不明白错误在哪里。您可以转换的最大双倍似乎是as.hexmode(as.double(2147483647))。你的价值比这大吗?

标签: r hex


【解决方案1】:

最后自己写了十六进制打印代码:

HexChar<-function(number) {
  x<-as.integer(number%%16)
  str<-as.character(as.hexmode(x))
  return (str)
}

HexString<-function(numberX) {
  number <- as.double(numberX)
  stringy<-""
  for (i in 1:16) {
    stringy<-paste(HexChar(number), stringy, sep="")
    number <- number/16
  }
  return (stringy)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-24
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    相关资源
    最近更新 更多