0

I have a block of code:

temp = "Cancel"
puts CGI::escape(words[1])
puts "\n"
puts CGI::escape(temp)
puts "\n"
puts words[1]
puts "\n"
puts temp
puts "\n"

My output is:

%00C%00a%00n%00c%00e%00l%00

Cancel

Cancel

Cancel

I think it's fair to assume that the issue here is the way I set up my words array. However, I was wondering if this is common behavior which has a solution? If not, what could I be doing wrong that would cause this?

My words array is set up by reading data from a file, then splitting each line and extracting the information I need, so it's nothing too complex.

2
  • 2
    Could your string be in a 16 bit encoding? Commented Aug 30, 2012 at 23:46
  • @muistooshort, agreed. It has that UTF-16 smell. Commented Aug 30, 2012 at 23:57

1 Answer 1

3

You have NUL bytes in your string. puts just ignores them.

1.9.2p290 :016 > puts "Fo\0oooo"
Fooooo
 => nil

with inspect you can see them:

1.9.2p290 :017 > puts "Fo\0oooo".inspect
"Fo\u0000oooo"
 => nil 

and here the output of CGI::escape

1.9.2p290 :018 > puts CGI::escape("Fooo\0ooo")
Fooo%00ooo
 => nil

edit:

The quick and dirty solution would be to just remove them:

"Fooooo\0ooo".gsub(/\0/, "")
 => "Foooooooo"

but as you have NUL bytes in front of every char, you should better check your code for reading the file. If you'd provide the code, it would be easier to come up with a solution.

Sign up to request clarification or add additional context in comments.

3 Comments

A better solution would be to determine if the file is UTF-16, and then, if so, open it as such prior to reading it.
I wrote: but as you have NUL bytes in front of every char, you should better check your code for reading the file., that includes checking the encoding, doesn't it?
I tried opening the file as both UTF-16BE and UTF-16LE but both formats cause the following error: ASCII incompatible encoding needs binmode (ArgumentError). Opening as binary obviously wasn't much help so any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.