将十六进制格式转换为R中的浮点数 [英] Converting hex format to float numbers in R

查看:183
本文介绍了将十六进制格式转换为R中的浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否请您帮助我,我们如何将4字节的十六进制格式转换为R中的浮点数?例如,我想将"aec7a042"转移到80.39.经过大量搜索,我无法在R中找到任何内容,可以实现这种转换! C函数是BitConverter. ToSingle.但是我需要在R中做同样的事情吗? 有人可以帮我吗?

could you please someone help me how we can convert 4 byte hex format to float number in R? for example I want to transfer "aec7a042" to 80.39 . I could not find anything in R after lots of search to give me this conversion! The C function is BitConverter. ToSingle. But I need to do the same in R? Can anyone help me, please?

推荐答案

您可以使用readBin读取此值.看来您有一个4字节的带符号浮点值.您可以阅读以下内容:

You can read this value using readBin. It seems you have a 4-byte signed float value. You can read that with:

readBin("aec7a042", "double", size=4)
# [1] 80.39

如果这在您的R版本中不起作用,请尝试

If this doesn't work in your version of R, try

x <- "aec7a042"
readBin(as.raw(strtoi(substring(x, (step<-seq(1, nchar(x), by=2)), step+1), 16)), "double",n=1,size=4)
# or 
readBin(as.raw(strtoi(apply(matrix(strsplit(x,"")[[1]],2),2,paste, collapse=""), 16)), "double", size=4)

在这里,我们将字符串更明确地转换为字节的原始向量.

Here we more explicitly convert the character string to a raw vector of bytes.

这篇关于将十六进制格式转换为R中的浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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