I like to encode UTF-8 string. In my data the character are separated with =
for the function encoding
in R I need to separate them with \x
string <- "=2E=30=31=20=52=C3=A9=70=6F=6E=64=65=75=72"
x <- gsub("=", "\x", string)
Encoding(x)
Encoding(x) <- "latin1"
x
I tried to add one, two, three backslashes to escape. Put in round and square brackets. Add quotes. Put the argument fixed=F
. Read here, here and here and still have no clue how to do it.
Expected output:
.01 Répondeur
When I use two backslashes like Wiktor says and check with cat()
, there's only one backslash in the output, but it has no effect on encoding()
, only when I change it by hand.
Edit:
For example when I do this, it puts two backslashes and endcoding
doesn't work:
> gsub("=", "\\x", string, fixed=TRUE)
[1] "\\x2E\\x30\\x31\\x20\\x52\\xC3\\xA9\\x70\\x6F\\x6E\\x64\\x65\\x75\\x72"
The same with the suggestion from Aleksandr Voitov:
> gsub("=", "\\\\x", string)
[1] "\\x2E\\x30\\x31\\x20\\x52\\xC3\\xA9\\x70\\x6F\\x6E\\x64\\x65\\x75\\x72"