Search code examples
pythonrblobhexdumpdbisam

Shift one position behind in a raw conversion from a hex buffer


I've been trying to extract a table from a blob column in a database that was converted into a hex string in the process. I'm using R for all my job. A capture of the BLOB data column is highlighted below:

DATA column is a hex string coming from a BLOB column in a database

enter image description here

The problem is that the data when inspected in raw form is shifted one position ahead, completely modifying the conversion to my numeric arrays when I make the final conversion. A capture of the problem is highlighted below:

The first chunk is the right data, the second chunk is the data shifted one position ahead

enter image description here

My question is, how do I move the data one position behind?

Is there any package in R or Python capable of doing that shift?

I've been trying with hex2raw function from wkb package without success. Same with decode functions in Python.


Solution

  • In R, You can "shift" you data numerically

    data <- as.raw(sample(256,10) - 1)
    data2 <- as.raw((as.integer(data) * 16L) %% 256L + ( c(data[-1], 0L) %/% 16L)  )
    
    
    data
    #>  [1] 89 af 4f 94 66 e8 84 c1 93 01
    data2
    #>  [1] 9a f4 f9 46 6e 88 4c 19 30 10
    

    EDIT

    Other way:

    data3 <- as.raw(
      as.integer(rawShift(data, 4)) + 
      as.integer(rawShift(c(data[-1],as.raw(0)), -4)))