简体   繁体   English

如何 plot 使用 R 的十六进制值的直方图?

[英]How to plot a histogram with hexadecimal values using R?

For my bachelor thesis I want to display memory addresses in hexadecimal format (48 Bit) using R with a histogram.对于我的学士论文,我想使用带有直方图的 R 以十六进制格式(48 位)显示 memory 地址。

The hex values are stored in a csv file:十六进制值存储在 csv 文件中:

$ cat addresses.csv | head -n 4
local variable,static variable,dynamically allocated variable,base (main),printf (library)
0x7ffcfa7c8694,0x55c109737010,0x55c10a70fe80,0x55c1095348fa,0x7f7099a39f00
0x7ffc17929914,0x5572286a9010,0x5572287fde80,0x5572284a68fa,0x7f8308f18f00
0x7ffdd75d11a4,0x55f6a7eff010,0x55f6a8e6de80,0x55f6a7cfc8fa,0x7fbc7d08bf00

So I plotted the addresses as follows:所以我将地址绘制如下:

> data = read.csv("addresses.csv")
> str(data)
'data.frame':   203540 obs. of  5 variables:
 $ local.variable                : num  1.41e+14 1.41e+14 1.41e+14 1.41e+14 1.41e+14 ...
 $ static.variable               : num  9.43e+13 9.39e+13 9.45e+13 9.41e+13 9.39e+13 ...
 ...
> hist(local.variable)

Result:结果:

十进制转换为十六进制的直方图

As you have probably noticed, the hexadecimal values were implicitly converted as decimals.您可能已经注意到,十六进制值被隐式转换为十进制。 Thats not what I was looking for.那不是我要找的。

How to plot a histogram with hexadecimal values?如何 plot 一个十六进制值的直方图?

My previous approach:我以前的做法:

> data = read.csv("addresses.csv", colClasses = "character")
> str (data)
 'data.frame':  203540 obs. of  5 variables:
 $ local.variable                : chr  "0x7ffcfa7c8694" "0x7ffc17929914" "0x7ffdd75d11a4" "0x7ffee91b85e4" ...
 $ static.variable               : chr  "0x55c109737010" "0x5572286a9010" "0x55f6a7eff010" "0x5592c9774010" ...
 ...
> hist(local.variable)
Error in hist.default(data$local.variable) : 'x' must be numeric

I look forward to your ideas and sophisticated workarounds.我期待您的想法和复杂的解决方法。 Thank you.谢谢你。

Edit: As requested sample data in reproducible format:编辑:按要求以可重现格式提供样本数据:

> data = read.csv("sample.csv")
> dput(data)
structure(list(local.variable = c(140724510951060, 140720703969556, 
140728216654244), static.variable = c(94287575609360, 93948792705040, 
94517867835408), dynamically.allocated.variable = c(94287592226432, 
93948794101376, 94517884018304), base..main. = c(94287573502202, 
93948790597882, 94517865728250), printf..library. = c(140121590701824, 
140200767491840, 140447528304384)), .Names = c("local.variable", 
"static.variable", "dynamically.allocated.variable", "base..main.", 
"printf..library."), class = "data.frame", row.names = c(NA, 
-3L))

> data = read.csv("sample.csv", colClasses = "character")
> dput(data)
structure(list(local.variable = c("0x7ffcfa7c8694", "0x7ffc17929914", 
"0x7ffdd75d11a4"), static.variable = c("0x55c109737010", "0x5572286a9010", 
"0x55f6a7eff010"), dynamically.allocated.variable = c("0x55c10a70fe80", 
"0x5572287fde80", "0x55f6a8e6de80"), base..main. = c("0x55c1095348fa", 
"0x5572284a68fa", "0x55f6a7cfc8fa"), printf..library. = c("0x7f7099a39f00", 
"0x7f8308f18f00", "0x7fbc7d08bf00")), .Names = c("local.variable", 
"static.variable", "dynamically.allocated.variable", "base..main.", 
"printf..library."), class = "data.frame", row.names = c(NA, 
-3L))

You can get the base 10 breaks that R is using for the plot and then plot hexadecimal labels at those locations instead.您可以获得 R 用于 plot 的 10 进制中断,然后在这些位置获取 plot 十六进制标签。 For example:例如:

# Fake data
set.seed(2)
x=as.hexmode(sample(1:1e9, 10000))

p = hist(x, xaxt="n")

Now, if you type p in the console, you'll see that it's a list with several elements.现在,如果您在控制台中键入p ,您会看到它是一个包含多个元素的列表。 One of these is called breaks and it contains a vector of the break values for the histogram.其中之一称为breaks ,它包含直方图中断值的向量。 We can use that to create hexadecimal labels and add them to the plot.我们可以用它来创建十六进制标签并将它们添加到 plot。

labs = as.hexmode(p$breaks)
axis(side=1, at=p$breaks, labels=labs)

在此处输入图像描述

The base 10 breaks won't in general be round numbers in base 16. If you want to set the breaks to round base 16 numbers, you could do something like this:以 10 为基数的换行符通常不会是以 16 为底数的整数。如果你想将换行符设置为以 16 为底数的整数,你可以这样做:

# Generate nice breaks in hexadecimal
brks = seq(as.hexmode(0), as.hexmode(round(1.01*max(x))), by=as.hexmode("4000000"))

p = hist(x, xaxt="n", breaks=brks)

axis(side=1, at=brks, labels=as.hexmode(brks))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM