How to Convert a Hex String to Text in R

How to convert a hex string to text in R?

Here's one way:

s <- '1271763355662E324375203137'
h <- sapply(seq(1, nchar(s), by=2), function(x) substr(s, x, x+1))
rawToChar(as.raw(strtoi(h, 16L)))

## [1] "\022qv3Uf.2Cu 17"

And if you want, you can sub out non-printable characters as follows:

gsub('[^[:print:]]+', '', rawToChar(as.raw(strtoi(h, 16L))))

## [1] "qv3Uf.2Cu 17"

Convert column of Hex into Text in R

If I understand this correctly, what you want to do it to apply a function to each element of a list so that it returns a character vector (that you can add to a data frame, if you so wish).

This can be easily accomplished with the purrr family of functions. The following takes each element df$Col_3 and runs the function (with each element being the x in the given function)

purrr::map_chr(.x = df$Col_3,
.f = function(x) {rawToChar(as.raw(strtoi(x,16L)))})

You should probably achieve the same with base R functions such as lapply() followed by unlist(), or sapply() but with purrr it's often easier to find inconsistent results.

Convert string to HEX in R

I have now found the perfect answer. So it should work on all systems:

function(x){
x%>%
str_extract_all(., "[:print:]")%>%
map(.x = ., ~stringi::stri_escape_unicode(.x))%>%
map(.x = ., ~str_replace_all(.x, "\\\\u0*", "&#x" ))%>%
map(.x = ., ~case_when(
str_detect(.x, "&#x") ~ str_c(.x, ";"),
T ~.x))%>%
map(.x =., ~str_c(.x, collapse = ""))%>%
unlist()
}

Thanks @MrFlick for your help!

How do I convert 64-bit hexadecimal strings in R?

For a number that large, you'll need to load a package that supports representations of arbitrarily large numbers. Rmpfr is one example:

library(Rmpfr)

## Check that it works as expected on smaller numbers:
strtoi("ff", base=16)
# [1] 255
mpfr("ff", base=16)
# 1 'mpfr' number of precision 8 bits
# [1] 255
as.integer(mpfr("ff", base=16)
# [1] 255

## Then apply it with (more) confidence to larger numbers:
mpfr("7f2d36a2a000", base=16)
# 1 'mpfr' number of precision 48 bits
# [1] 139832166883328
mpfr("7f2d36a2a0007f2d36a2a0007f2d36a2a0007f2d36a2a000", base=16)
# 1 'mpfr' number of precision 192 bits
# [1] 3118361524223520784583964884878580812558070356334996529152

Convert hex to decimal in R

Use base::strtoi to convert hexadecimal character vectors to integer:

strtoi(c("0xff", "077", "123"))
#[1] 255 63 123

How to convert hex data into real numbers with R

You can use the simple python functions. I have stored the Hexa in a variable. then you can use the split function to convert hex in the list as below. To transfer the list of Hexa in numbers use a print function with for loop.

1. For i.e. hex_string = "your hexa data" 
2. For i.e. ist = (hex_string.split(" "))

Check the below for the entire code.

hex_string = "0400 0000 cf40 2761 d2ff 5552 ceff 5652 e0ff 5b52 e0ff 5b52 
eaff 5f52 f4ff 5b52 eaff 5852 eeff 5752 f2ff 5952 eeff 5a52 d8ff 5452
d4ff 5652 caff 5452 ccff 5352 d2ff 5552 c2ff 5a52 ccff 5e52 e2ff 5652
eaff 5852 f0ff 5652 e8ff 5452 e8ff 5452 eaff 5552 deff 5a52 f0ff 5f52
faff 5b52 0a00 6152 0400 6352 f8ff 5f52 0000 5c52 f8ff 5952 0200 5452
0000 5452 f2ff 5952 f8ff 5a52 0400 5652 0400 5652 f2ff 5c52"

list = (hex_string.split(" "))
for i in list:
print(int(i,16))


Related Topics



Leave a reply



Submit