• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Decoding "%5c"

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am using URLDecoder.decode(String, "UTF-8") for decoding url encoded strings. I used encodeURIComponent() for encoding the strings in javascript. the decode method is not decoding "\" correctly. "\" is getting encoded as %5c", but when I pass the string to decode, it is getting decoded as "\\". I am seeing that for each "\" I enter it is decoding as double characters "\\", so if I enter "\\", which is "%5c%5c"
I am seeing it is decoded as "\\\\". How can I decode "\" correctly.

Thanks
 
Marshal
Posts: 73979
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I saw your posting earlier and thought somebody else would answer better than I can, but I shall try.

You know that \ is a sort of meta-character in Java strings; it "escapes" the character following. If I remember correctly there are about 8 escape combinations:
  • \b Backspace
  • \t Tab (horizontal)
  • \n newline (or more precisely the line-feed character 0x0a)
  • \r return (the carriage return character 0x0d)
  • \f form feed
  • \' Single quote (not smart quotes)
  • \" Double quote (not smart quotes)
  • \\ Backslash
  • You can also insert a Unicode or octal character with an escape sequence like \u1234 or\123. So it is not at all surprising that Java will translate a backslash to \\. That is the normal Java way of writing backslash.
     
    Ranch Hand
    Posts: 1970
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Campbell Ritchie:
    You know that \ is a sort of meta-character in Java strings; it "escapes" the character following.



    Backslash is a special character in Java string literals (and char literals, too). That is, when you want to type a fixed string into your source code, you have to use backslash escape codes for some special characters (including backslash).

    Once the characters are stored in a Java String object on the heap, the special meaning of backslash is gone. A backslash in a Java String object is just a backslash, nothing more.
    [ October 16, 2007: Message edited by: Peter Chase ]
     
    Campbell Ritchie
    Marshal
    Posts: 73979
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You have put it rather better than I did, Mr Chase.
     
    Java Cowboy
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Saat,

    Are you sure that URLDecoder.decode(...) decodes "%5c" to \\ instead of \? I wrote a small program to check it, and it prints a single backslash, not two backslashes.

    How are you displaying the decoded string?

    This prints just a single backslash:
     
    If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic