0

I'm trying to convert some text from ebcdic. This does the job just fine:

> iconv( "\xf0\xc1\xf0\xe5", from="IBM01047", to="ASCII", sub="?"  )
[1] "0A0V"

But I'm struggling to prepare the correct input variable using paste0:

s <- "f0c1f0e5"
> s2 <- sapply(seq(1, nchar(s), by=2), function(x) paste0( "\\x", substr(s, x, x+1)) )
> iconv( s2, from="IBM01047", to="ASCII", sub="?"  )
[1] "*???" "*???" "*???" "*???"
> s2
[1] "\\xf0" "\\xc1" "\\xf0" "\\xe5"
> cat(s2)
\xf0 \xc1 \xf0 \xe5

What do I need to do to the output of paste0 to get iconv() to accept it? cat() seems to be able to do it.

There are a number of paste0/backslash questions here, but none seem to address this particular issue.

Update:

Tried with raw strings, with exactly the same result:

s2 <- sapply(seq(1, nchar(s), by=2), function(x) paste0( r"(\x)", substr(s, x, x+1)) )
Ian
  • 1,507
  • 3
  • 21
  • 36
  • Why would you want this? can you create `A` from pasting things together? or simply put do you think you can create a line feed by pasting `\ ` and `n`? Note that even though `\n` seems to be two characters put together, it is technically just one character just like `a` or `A` is 1 character. eg `nchar('\n')` returns 1. So simply put, you can not combine `\ ` and `n` to `\n` because the former are two characters while the latter is just 1 character. Similarly a hexadecimal number such as `\xf0` is just but 1 character just as `7` is one character. eg `nchar('\xf0')` is 1. – Onyambu Jun 21 '22 at 05:42
  • Hence what you are asking is not impossible but also illogical. We must have the base belief foundation for everything. We know what `1` is, we dont ask anything about how or why 1 is 1. We dont create it from combining other stuff. But we take it on face value. From there we can build other numbers (greater than 9, using 0-9) from it ie 11, 21 etc. – Onyambu Jun 21 '22 at 05:45
  • @onyambu Not sure why you think it's illogical. I need to transform a string of hex characters representing ebcidic characters, eg "f0c1f0e5" as given in the question, to the ascii text string "0A0V". This requires prefixing each par of characters with "\x" and passing the resulting string into iconv. If you can suggest a better way of doing it I'm just as happy! – Ian Jun 21 '22 at 06:02
  • but you cannot *create* a string of one character from two characters. eg you cannot create 'A' or even create 'B' for example. Though We can create a string with two characters from the individual characters. eg you can create `AB` from `A` and `B`. In every language, there must be the smallest unit that cannot be broken further. in english language we have `A-Z` we cannot create those. but we can use them to create other things. In math base 10 e have 0-9. These are DEFINED as such. similarly `\xf` is defined as such, `\xf0` is defined as such. It is one character just like `A` – Onyambu Jun 21 '22 at 06:19

1 Answers1

0

A combination of the answers to How to convert a hex string to text in R? gives me something I can feed into iconv() sucessfully, though it doesn't really answer the question as to why I can't achieve this with paste0():

ebcdicHexPairsToAscii = function(textHexPairs){
  hexRaw <- wkb::hex2raw(textHexPairs)
  hexString <- rawToChar(as.raw(strtoi(hexRaw, 16L)))
  iconv( hexString, from="IBM01047", to="ASCII", sub="?"  )
}

hex <- "f0c1f0e5"
> ebcdicHexPairsToAscii(hex)
[1] "0A0V"
Ian
  • 1,507
  • 3
  • 21
  • 36