0

I have a string like:

a = "@\n\u001E\rANSI 636000410225ZM02660027DLDCA\nDCB\nDCD\nDBA04052024\nDCSKILLER01-21-2014\nZMB01\n\r"

I'm following this SO link for fix, but no matter what I try, the \u001E doesn't get resolved to proper string.

What am I doing wrong?

Sample Output

=> a.unpack("U*").map{|c|c.chr}.join

=> "@\n\x1E\rANSI 636000410225ZM02660027DLDCA\nDCB\nDCD\nDBA04052024\nDCSKILLER01-21-2014\nZMB01\n\r"

Same output for: a.encode('ASCII')

Any suggestions?

Abhi
  • 4,123
  • 6
  • 45
  • 77
  • It’s not clear what you want to do here. `\u001E` already is an ASCII character. It is the control character “Record Separator”. In both ASCII and UTF-8 it consists of the single byte `0x1E`. Ruby will display it as either `\u001E` if the string has UTF-8 encoding and as `\x1E` if the string has ASCII (or binary) encoding. – matt Sep 25 '20 at 14:47
  • @matt How to get rid of it without using manual ways like `gsub`? In the entire string, that is the only unwanted portion. – Abhi Sep 25 '20 at 15:07
  • Just using `gsub` (or `sub` if there’s only one of these characters) is probably the way to go here. Theres nothing really special about `\u001E`, it’s just a character like any other, albeit a non printing one. – matt Sep 25 '20 at 23:59

0 Answers0