你可以使用R中的以下方法从十六进制中获取32位小端值:

huangapple go评论88阅读模式
英文:

How can I get the 32-bit little-endian value from hex in R?

问题

I have a data file with the following hex values in bytes 17:20
> 92 02 00 00

According to https://hexed.it/, the 32-bit integer of these bytes (signed or unsigned) is 658. How can I get the value 658 from these raw values? Note that this is Little-endian ordered.

  1. > readBin(img_path, "raw", 20)[17:20]
  2. [1] 92 02 00 00

你可以使用R中的以下方法从十六进制中获取32位小端值:

根据 https://hexed.it/ 的信息,这些字节的32位整数(有符号或无符号)是658。如何从这些原始值中获取值658?请注意这是小端序排列。

  1. > readBin(img_path, "raw", 20)[17:20]
  2. [1] 92 02 00 00

你可以使用R中的以下方法从十六进制中获取32位小端值:

英文:

I have a data file with the following hex values in bytes 17:20
> 92 02 00 00

According to https://hexed.it/, the 32-bit integer of these bytes (signed or unsigned) is 658. How can I get the value 658 from these raw values? Note that this is Little-endian ordered.

  1. > readBin(img_path, "raw", 20)[17:20]
  2. [1] 92 02 00 00

你可以使用R中的以下方法从十六进制中获取32位小端值:

答案1

得分: 1

如果你将文件作为字符读取,你可以使用 strtoi 函数。

  1. hex_to_int = function(x) {
  2. i = 256^(0:(length(x)-1)) * strtoi(x, base = 16L)
  3. sum(i)
  4. }
  5. x = c("92", "02", "00", "00")
  6. hex_to_int(x)
  7. # [1] 658

希望这对你有所帮助。

英文:

If you read from the file as character, you can use strtoi

  1. hex_to_int = function(x) {
  2. i = 256^(0:(length(x)-1)) * strtoi(x, base = 16L)
  3. sum(i)
  4. }
  5. x = c("92", "02", "00", "00")
  6. hex_to_int(x)
  7. # [1] 658

答案2

得分: 1

如果您有一个原始向量,您可以使用readBin函数将其转换为整数。例如:

  1. x <- as.raw(c(146, 2, 0, 0))
  2. x
  3. # [1] 92 02 00 00
  4. readBin(x, integer(), n=1, size=4, endian = "little")
  5. # [1] 658

请注意,以上是代码示例,用于将原始向量转换为整数。

英文:

If you have a raw vector you can use readBin to convert to integer. For example

  1. x &lt;- as.raw(c(146, 2, 0, 0))
  2. x
  3. # [1] 92 02 00 00
  4. readBin(x, integer(), n=1, size=4, endian = &quot;little&quot;)
  5. # [1] 658

答案3

得分: 0

以下是代码的翻译部分:

  1. # 一种手动执行的方法是:
  2. get_32bit_unsigned <- function(raw_32bit) {
  3. if (!(inherits(raw_32bit, 'raw') & length(raw_32bit) == 4)) {
  4. rlang::abort('raw_32bit必须是一个32位原始向量')
  5. }
  6. width_in_bits = rawToBits(raw_32bit)
  7. v = 0
  8. for (i in 1:length(width_in_bits)) {
  9. v = v + 2^(i-1) * as.integer(width_in_bits[i])
  10. }
  11. return(v)
  12. }
  13. get_32bit_unsigned(readBin(img_path, "raw", 20)[17:20])
  14. # 输出:658

希望这对你有所帮助。如果你需要进一步的帮助,请随时提问。

英文:

One way to do this is manually:

  1. get_32bit_unsigned &lt;- function(raw_32bit) {
  2. if(!( inherits(raw_32bit, &#39;raw&#39;) &amp; length(raw_32bit) == 4)) {
  3. rlang::abort(&#39;raw_32bit must be a 32-bit raw vector&#39;)
  4. }
  5. width_in_bits = rawToBits(raw_32bit)
  6. v = 0
  7. for(i in 1:length(width_in_bits)) {
  8. v = v + 2^(i-1)*as.integer(width_in_bits[i])
  9. }
  10. return(v)
  11. }
  12. get_32bit_unsigned(readBin(img_path, &quot;raw&quot;, 20)[17:20])
  13. &gt; 658

huangapple
  • 本文由 发表于 2023年3月4日 05:24:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定