File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Decoding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Decoding "%5c"" Watch "Decoding "%5c"" New topic

Decoding "%5c"

saat reddy

Joined: Oct 15, 2007
Posts: 1

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.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45421
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.
    Peter Chase
    Ranch Hand

    Joined: Oct 30, 2001
    Posts: 1970
    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 ]

    Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
    Campbell Ritchie

    Joined: Oct 13, 2005
    Posts: 45421
    You have put it rather better than I did, Mr Chase.
    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 15047


    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:

    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
    I agree. Here's the link:
    subject: Decoding "%5c"
    It's not a secret anymore!