Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7864625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:50:22+00:00 2026-06-02T23:50:22+00:00

I am using URI.unescape to unescape text, unfortunately I run into weird error: #

  • 0

I am using URI.unescape to unescape text, unfortunately I run into weird error:

 # encoding: utf-8
 require('uri')
 URI.unescape("%C3%9Fą")

results in

 C:/Ruby193/lib/ruby/1.9.1/uri/common.rb:331:in `gsub': incompatible character encodings: ASCII-8BIT and UTF-8 (Encoding::CompatibilityError)
    from C:/Ruby193/lib/ruby/1.9.1/uri/common.rb:331:in `unescape'
    from C:/Ruby193/lib/ruby/1.9.1/uri/common.rb:649:in `unescape'
    from exe/fail.rb:3:in `<main>'

why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:50:24+00:00Added an answer on June 2, 2026 at 11:50 pm

    The implementation of URI.unescape is broken for non-ASCII inputs. The 1.9.3 version looks like this:

    def unescape(str, escaped = @regexp[:ESCAPED])
      str.gsub(escaped) { [$&[1, 2].hex].pack('C') }.force_encoding(str.encoding)
    end
    

    The regex in use is /%[a-fA-F\d]{2}/. So it goes through the string looking for a percent sign followed by two hex digits; in the block $& will be the matched text (‘%C3’ for example) and $&[1,2] be the matched text without the leading percent sign ('C3'). Then we call String#hex to convert that hexadecimal number to a Fixnum (195) and wrap it in an Array ([195]) so that we can use Array#pack to do the byte mangling for us. The problem is that pack gives us a single binary byte:

    > puts [195].pack('C').encoding
    ASCII-8BIT
    

    The ASCII-8BIT encoding is also known as “binary” (i.e. plain bytes with no particular encoding). Then the block returns that byte and String#gsub tries to insert into the UTF-8 encoded copy of str that gsub is working on and you get your error:

    incompatible character encodings: ASCII-8BIT and UTF-8 (Encoding::CompatibilityError)

    because you can’t (in general) just stuff binary bytes into a UTF-8 string; you can often get away with it:

    URI.unescape("%C3%9F")         # Works
    URI.unescape("%C3µ")           # Fails
    URI.unescape("µ")              # Works, but nothing to gsub here
    URI.unescape("%C3%9Fµ")        # Fails
    URI.unescape("%C3%9Fpancakes") # Works
    

    Things start falling apart once you start mixing non-ASCII data into your URL encoded string.

    One simple fix is to switch the string to binary before try to decode it:

    def unescape(str, escaped = @regexp[:ESCAPED])
      encoding = str.encoding
      str = str.dup.force_encoding('binary')
      str.gsub(escaped) { [$&[1, 2].hex].pack('C') }.force_encoding(encoding)
    end
    

    Another option is to push the force_encoding into the block:

    def unescape(str, escaped = @regexp[:ESCAPED])
      str.gsub(escaped) { [$&[1, 2].hex].pack('C').force_encoding(encoding) }
    end
    

    I’m not sure why the gsub fails in some cases but succeeds in others.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We could insert data into database without using the Uri. But when we are
I'm using an Uri returned from image gallery to create a Bitmap. I have
I am attempting to print a JPEG file that I reference using a Uri
My domain model is using System.Net.Uri to represent URLs, and System.Drawing.Color to represent colors.
I'm using Nokogiri and open-uri to grab the contents of the title tag on
I am using an AsyncPlayer to stream music from an URI and when testing
I'm using a library that requires me to pass an Uri object as parameter.
How can I translate whole URI in routes using I18n? Example: (CZ URI =>
I tried accessing web for xml parsing using the code below: System.Uri proxy =
I'm using intents to launch Google Navigation: Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(google.navigation:q= +

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.