Reputation: 33
For my bachelor thesis I want to display memory addresses in hexadecimal format (48 Bit) using R with a histogram.
The hex values are stored in a csv file:
$ 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?
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))
Upvotes: 1
Views: 414
Reputation: 93871
You can get the base 10 breaks that R is using for the plot and then plot hexadecimal labels at those locations instead. 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. One of these is called breaks
and it contains a vector of the break values for the histogram. We can use that to create hexadecimal labels and add them to the 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:
# 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))
Upvotes: 3