File Packing Ideas

Greetings,

I am looking for help to solve this problem I have. I need to read in a
file, and convert it into a single line string with no characters readable
in it, (such as \n, \t, etc.) so that it can be sent over a socket
connection and reconverted on the other side.

I have tried pack, Marshal, and gsub, and different combinations. The
closest I got was readline-ing the file into an array, chomping the newlines
out, and marshalling the array into a string. This seemed to work until I
tried to send a \n within the text. So I added a gsub(/\n/, ‘0x0a’) when
pushing into the array. This seemed to work until I sent a \t, or maybe it
was the Marshal.dump() text that was sent?!?

Does anyone have any good ideas how I can pack all my files into a one-liner
string? Or will I have to go beg A*L for their latest protocol and see how
they do it? :slight_smile:

Thanks,
BubbaJoeLouis

The CGI class solves this problem for you: URL’s need to be escaped as
well.

irb(main):001:0> require ‘cgi’
true
irb(main):002:0> str = “\n\f\r\b”
“\n\f\r\010”
irb(main):003:0> str2 = CGI.escape str
“%0A%0C%0D%08”
irb(main):004:0> str == CGI.unescape(str2)
true
irb(main):005:0>

– Nikodemus

···

On Wed, 13 Nov 2002, Vera, Michael wrote:

I am looking for help to solve this problem I have. I need to read in a
file, and convert it into a single line string with no characters readable
in it, (such as \n, \t, etc.) so that it can be sent over a socket
connection and reconverted on the other side.