在R中使用as.hexmode来处理double [英] Using as.hexmode in R to handle a double

查看:245
本文介绍了在R中使用as.hexmode来处理double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中有 as.hexmode 间歇性问题。



以前的问题我现在有代码如下所示:

  WriteOutRW < - 函数(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)))



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

 <指令地址=4d0bffsize =3/> 
<指令地址=4d0c02大小=2/>
< load address =4e36e0size =c/>
< load address =4e36ecsize =1/>
<负载地址=4e36ed size =1/>
< load address =4e36eesize =2/>
< instruction address =4d0328size =2/>
< instruction address =4d032asize =2/>
< modify address =4df2ddsize =1/>
< load address =4df2desize =1/>
< modify address =4df2dfsize =2/>
< instruction address =4d0204size =2/>
< instruction address =4d0206size =1/>
< instruction address =4d0207size =1/> (x)&&(x == as.integer(x)))x < - as.integer(x):$ b $中的错误
< load address = b缺少值,其中TRUE / FALSE需要
调用:WriteOutRW - > cat - > as.hexmode
此外:警告消息:
在as.hexmode(writePoint)中:强制
执行停止

指令元素是由一个非常相似的函数产生的 - 尽管地址永远不会是双重的 - 并且它永远不会失败。我的假设是 WriteOutRW当 writePoint 是double时,函数失败,但是 as.hexmode 应该处理双精度,应该不是吗?

更新为了清楚我正在尝试在这里处理整数(Integer和Double类型)和数字print作为10位小数没有问题,但我真的想要十六进制输出。

解决方案

编写我自己的十六进制打印代码

  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)
}


I have an intermittent problem with as.hexmode in R.

Following good advice in response to a previous question I now have code that looks like this:

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)))
  .
  .
  .

This produces output like this...

<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

The instruction elements are produced by a very similar function - though the address there can never be a double - and it never fails. My assumption is that the WriteOutRW function is failing when writePoint is a double. But as.hexmode should handle doubles, shouldn't it?

update Just to be clear I am trying to process integers here (of type Integer and Double) and the numbers print out as base 10 decimals without a problem, but I really want hex output.

解决方案

Wrote my own hex printing code in the end:

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)
}

这篇关于在R中使用as.hexmode来处理double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆