File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Decoding "%5c"

 
saat reddy
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • 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.
     
    Peter Chase
    Ranch Hand
    Posts: 1970
    • 0
    • Mark post as helpful
    • send pies
    • 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
    Sheriff
    Pie
    Posts: 47300
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You have put it rather better than I did, Mr Chase.
     
    Jesper de Jong
    Java Cowboy
    Saloon Keeper
    Pie
    Posts: 15150
    31
    Android IntelliJ IDE Java Scala Spring
    • 0
    • Mark post as helpful
    • send pies
    • 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:
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic