writeBin raw() different for 1:3 and c(1,2,3)? - r

> writeBin(1:3, raw(), size=4, endian="little")
[1] 01 00 00 00 02 00 00 00 03 00 00 00
> writeBin(c(1,2,3), raw(), size=4, endian="little")
[1] 00 00 80 3f 00 00 00 40 00 00 40 40
> writeBin(c(1:3), raw(), size=4, endian="big")
[1] 00 00 00 01 00 00 00 02 00 00 00 03
> writeBin(c(1,2,3), raw(), size=4, endian="big")
[1] 3f 80 00 00 40 00 00 00 40 40 00 00
The first and third result is what I expect, but why do I get a different raw vector for a vector specified as c(1,2,3)?

Probably because one is stored as an integer, and the other as a double:
typeof(1:3)
typeof(c(1,2,3))
typeof(c(1L,2L,3L))

Related

Hex to datetime from a BLE custom service

I'm trying to decode a hex string to a datetime format from a BLE custom UUID. I don't have any information except the decoded value and datetime, that's why I put similar examples in milliseconds and minutes. It will be complicated to see the value of another year.
If you look closely, you will notice there are only 2 values repeated twice in each row (bold and italic). First starts with 0x01 (maybe a flag) and then 2 bytes and second 2 bytes too.
Similar minutes:
01 8E 6C 00 00 8E 6C 00 00 62 25 62 25 00 00 00 00 00 00 00 - 1669024172,5141 (Nov 21 2022 09:49:32,5387632)
01 9C 6C 00 00 9C 6C 00 00 62 49 62 49 00 00 00 00 00 00 00 - 1669024172,68598 (Nov 21 2022 09:49:32,7638451)
01 fd 6c 00 00 fd 6c 00 00 64 0f 64 0f 00 00 00 00 00 00 00 - 1669024174,23293 (Nov 21 2022 09:49:34,3170414)
01 0d 6d 00 00 0d 6d 00 00 6e 2f 6e 2f 00 00 00 00 00 00 00 - 1669024174,5767 (Nov 21 2022 09:49:34,5414078)
01 f0 6d 00 00 f0 6d 00 00 62 21 62 21 00 00 00 00 00 00 00 - 1669024178,01436 (Nov 21 2022 09:49:38,0746688)
01 00 6e 00 00 00 6e 00 00 67 81 67 81 00 00 00 00 00 00 00 - 1669024178,35812 (Nov 21 2022 09:49:38,2775026)
Similar milliseconds:
01 16 1d 00 00 16 1d 00 00 4d f4 4d f4 00 00 00 00 00 00 00 - 1669023854,70251 (Nov 21 2022 09:44:14,7728813)
01 1e 6e 00 00 1e 6e 00 00 67 c5 67 c5 00 00 00 00 00 00 00 - 1669024178,70189 (Nov 21 2022 09:49:38,7714679)
01 e2 6d 00 00 e2 6d 00 00 62 4d 62 4d 00 00 00 00 00 00 00 - 1669024177,84247 (2022-11-21 10:49:37,8263242)
01 5f dd 06 00 5f dd 06 00 cb 46 cb 46 00 00 00 00 00 00 00 - 1669110577,66758 (2022-11-22 10:49:37,8231004)
01 00 6e 00 00 00 6e 00 00 67 81 67 81 00 00 00 00 00 00 00 - 1669024178,35812 (2022-11-21 10:49:38,2775026)
01 7c dd 06 00 7c dd 06 00 c8 0c c8 0c 00 00 00 00 00 00 00 - 1669024178      (2022-11-22 10:49:38,2722736)
It's looks like:
It's not decoded in the unix epoch (JAN 1 1970)
The first value is increasing (0xXX6C, 0xXX6E, 0xXX6E), so maybe it's the int part.
I tried to decode like year, minutes, seconds but it seems it isn't.
Sometimes first value is bigger than 2 bytes (3 bytes):
01 DE 61 04 00 DE 61 04 00 4A C1 4A C1 00 00 00 00 00 00 00 - 1669104436 (Nov 22 2022 09:07:16,1902042)
I tried this solutions:
https://stackoverflow.com/questions/1389046/what-is-the-specification-of-hexadecimal-date-format-in-sql-server Error: System.ArgumentOutOfRangeException (trying one of my hex)
http://erbhavi.blogspot.com/search/label/Converting%20DateTime%20to%20Hex%20in%20C%23 DateTime yourDateTime = new DateTime( ticks1970 + gmt * 10000000L ); //Error CS0019: Operator '*' can't apply to types 'string' y 'long'
I'll update the question if I find more info.

Split all values in columns to create new columns

I have a data frame of integers, which I would like to convert to bits, and then split each resulting bit sequence into separate columns. I figured I could convert my bits to string and then split the strings on whitespace. However, my method does not work.
I create my data frame:
r1 <- c(24,25,27)
r2 <- c(98,102,4)
model.data <- as.data.frame(rbind(r1,r2))
and then I convert int to bits with:
model.data[] <- lapply(model.data, function(x) {
if(!is.na(x)) as.character(paste(intToBits(x), collapse = " ")) else x
})
However, this is where I encounter my first problem. The second row of my data frame now seems to have the same values as the first one:
model.data$V1
[1] "00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
[2] "00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
This shouldn't be the case, since the values in the original data frame differ. It's the same case for all the columns here (V2, V3).
My second problem is that I can't split the columns. I try this method:
for(i in names(model.data)) {
a.split <- strsplit(model.data[[i]], split = " ")
cbind(model.data,a.split)
}
but it does nothing.
Could you help me figure out what I'm doing wrong? Thanks!
Use the function "separate" from tidyr package
library(tidyr)
# Your data
r1 <- c(24,25,27)
r2 <- c(98,102,4)
# Concatenate r1 and r2
r3 <- c(r1,r2)
# Create a dataframe
model.data <- as.data.frame(r3)
# Check type of r3
class(model.data$r3)
# Transform with "intToBits" function
model.data[] <- lapply(model.data, function(x) {
if(!is.na(x)) as.character(paste(intToBits(x), collapse = " ")) else x
})
nb = length(unlist(strsplit(model.data$r3[1]," ")))
# Transform into character
model.data$r3 <- as.character(model.data$r3)
# Use "separate" function from tidyr, generating nb variable
model.data %>% separate(r3, into = paste0("V",seq(1, nb, by = 1)), sep = " ")
You need to apply over both rows and columns, using ifelse. Interestingly, using lapply does not seem to work. Here an example for the apply solution:
r1 <- c(24,25,27)
r2 <- c(98,102,4)
model.data <- as.data.frame(rbind(r1,r2))
model.data[] <- as.data.frame(apply(model.data, c(1,2), function(x) ifelse(!is.na(x), as.character(paste(intToBits(x), collapse = " ")), x)), stringsAsFactors = FALSE)
> model.data
V1
r1 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
r2 00 01 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
V2
r1 01 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
r2 00 01 01 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
V3
r1 01 01 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
r2 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Edit:
To create the new data with one bit per variable, first lapply the strsplit, giving you a nested list as output, then use a nested do.call to first cbind the sublists, then rbind the result:
newCols <- lapply(model.data, function(x) strsplit(x, split = " "))
a.split <- as.data.frame(do.call(rbind, do.call(cbind, newCols)), stringsAsFactors = FALSE)
> a.split
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22 V23 V24 V25 V26 V27 V28 V29 V30 V31 V32
1 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2 00 01 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
3 01 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
4 00 01 01 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
5 01 01 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
6 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

the meaning of bits in rawToBits?

> as.raw(15)
[1] 0f
> rawToBits(as.raw(15))
[1] 01 01 01 01 00 00 00 00
> rawToBits(0f)
Error: unexpected symbol in "rawToBits(0f"
> rawToBits("0f")
Error in rawToBits("0f") : argument 'x' must be a raw vector
> rawToBits("0x0f")
Error in rawToBits("0x0f") : argument 'x' must be a raw vector
I have some problems to ask:
1) is that 0f a raw type data?
2) why rawToBits(as.raw(15)) can not get 11110000? the 15 is not 11110000?
15=0f=1*2^0+1*2^1+1*2^2+1*2^3
What is the meaning of 0 in [1] 01 00 00 00 00 00 00 00 when you input rawToBits(as.raw(1))?
In the manual ,i get a raw vector with entries 0 or 1,what is the meaning ofentries 0 or 1.
Why rawToBits(as.raw(2)) is not 10 00 00 00 00 00 00 00?
Just typing 0f doesn't give you something of type raw.
> str(as.raw(15))
raw 0f
> str(0f)
Error: unexpected symbol in "str(0f"
> str("0f")
chr "0f"
If you want to know what's going on with the bits you could try some other values to get a better idea what is going on
> rawToBits(as.raw(1))
[1] 01 00 00 00 00 00 00 00
> rawToBits(as.raw(2))
[1] 00 01 00 00 00 00 00 00
> rawToBits(as.raw(4))
[1] 00 00 01 00 00 00 00 00
> rawToBits(as.raw(8))
[1] 00 00 00 01 00 00 00 00
> rawToBits(as.raw(1 + 2 + 4 + 8))
[1] 01 01 01 01 00 00 00 00
> rawToBits(as.raw(15))
[1] 01 01 01 01 00 00 00 00

the bits in R to express integer

R use 32-bit integers, 4 bytes (4*8=32, 1 byte=8 bits),max integer is 2^31-1= 2147483647.
intToBits(125)
[1] 01 00 01 01 01 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
There are 64 bits ,not 32 bits,32 pairs of 00 or 01(32*2=64) ,why?
in the manual ,a raw vector with entries 0 or 1,what is the meaning of entries 0 or 1?
In my opinion, intToBits(125) will generate
10111110 00000000 00000000 00000000 # it is 32 bits
not
01 00 01 01 01 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00. # it is 64 bits
Function intToBits returns 32 numbers, each 0 or 1.
> length(intToBits(125))
[1] 32
The confusion is caused by the hex representation of the raw type of the output of intToBits.
> intToBits(125)[3]
[1] 01
The raw numbers 0 or 1 are shown as 00 or 01.

Comparing two mach-o files

I have two mach-o files and i need to find the difference(hexadecimal differed values) in them. is there any tool available for doing this.
i tried using "DiffMerge" but it doesn't have the supported encoding format it seems.
how about:
hexdump binary1 > dump1.txt
hexdump binary2 > dump2.txt
diff dump1.txt dump2.txt
This will give you a diff file and you can use the offset in the first column in hexfiend or whatever your choice of editor is to investigate further.
2c2
< 0000010 21 00 00 00 bc 0f 00 00 85 00 20 00 01 00 00 00
---
> 0000010 20 00 00 00 84 0f 00 00 85 00 20 00 01 00 00 00
248,254c248,250
< 0000f80 0c 00 00 00 38 00 00 00 18 00 00 00 02 00 00 00
< 0000f90 00 00 01 00 00 00 01 00 40 65 78 65 63 75 74 61
< 0000fa0 62 6c 65 5f 70 61 74 68 2f 69 6e 6a 65 63 74 2e
< 0000fb0 64 79 6c 69 62 00 00 00 26 00 00 00 10 00 00 00
< 0000fc0 58 76 18 00 a4 23 00 00 1d 00 00 00 10 00 00 00
< 0000fd0 e0 09 19 00 60 37 00 00 00 00 00 00 00 00 00 00
< 0000fe0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
---
> 0000f80 26 00 00 00 10 00 00 00 58 76 18 00 a4 23 00 00
> 0000f90 1d 00 00 00 10 00 00 00 e0 09 19 00 60 37 00 00
> 0000fa0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Resources