BASE64Encoder and BASE64Decoder

Discussion in 'Java' started by Vallabha, Sep 30, 2008.

  1. Vallabha

    Vallabha Guest

    Hello All,

    I am trying to understand how BASE64Encoder() and BASE64Decoder()
    work. I wrote a small sample for that:

    ==========================================

    import java.io.IOException;
    import java.util.*;

    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;

    public class Base64 {
    public static void main(String[] args)throws IOException{
    String a = new String("TEST");
    System.out.println("Original string: " + a);
    String encode = new sun.misc.BASE64Encoder().encode(a.getBytes());
    System.out.println("Encoded String: " + encode);
    byte[] decode = new sun.misc.BASE64Decoder().decodeBuffer(encode);
    System.out.println("Decoded String: " + decode);
    }
    }

    ========================================
    Original string: TEST
    Encoded String: VEVTVA==
    Decoded String: [B@addbf1
    =========================================

    Here I was expecting the original string after the decoding. However
    decoded string is different from the original one. Now how do I get
    back my original string.

    Any clue on this will be of great help.

    Thanks
    -Vallabha
    Vallabha, Sep 30, 2008
    #1
    1. Advertising

  2. Vallabha schrieb:
    > Hello All,
    >
    > I am trying to understand how BASE64Encoder() and BASE64Decoder()
    > work. I wrote a small sample for that:
    >
    > ==========================================
    >
    > import java.io.IOException;
    > import java.util.*;
    >
    > import sun.misc.BASE64Decoder;
    > import sun.misc.BASE64Encoder;
    >
    > public class Base64 {
    > public static void main(String[] args)throws IOException{
    > String a = new String("TEST");
    > System.out.println("Original string: " + a);
    > String encode = new sun.misc.BASE64Encoder().encode(a.getBytes());
    > System.out.println("Encoded String: " + encode);
    > byte[] decode = new sun.misc.BASE64Decoder().decodeBuffer(encode);
    > System.out.println("Decoded String: " + decode);
    > }
    > }
    >
    > ========================================
    > Original string: TEST
    > Encoded String: VEVTVA==
    > Decoded String: [B@addbf1
    > =========================================
    >
    > Here I was expecting the original string after the decoding. However
    > decoded string is different from the original one. Now how do I get
    > back my original string.
    >


    What if you do this

    System.out.println("Decoded String: " + String.valueOf(decode));

    > Any clue on this will be of great help.
    >
    > Thanks
    > -Vallabha
    Stefan Rybacki, Sep 30, 2008
    #2
    1. Advertising

  3. Vallabha wrote:
    > byte[] decode = new sun.misc.BASE64Decoder().decodeBuffer(encode);
    > System.out.println("Decoded String: " + decode);


    What type is decode?

    > Here I was expecting the original string after the decoding. However
    > decoded string is different from the original one. Now how do I get
    > back my original string.


    Do a little experiment: Tell me what (new byte[0]).toString() is.

    Converting an array to a string via Object.toString() is not going to
    give you what you expect... you'll want to actually pass it into a
    byte[]->String converter, like |new String(decode)| or
    |String.valueOf(decode)|.

    What you're actually seeing is Object's toString implementation:
    getClass().getName() + "@" + Integer.toHexString(hashCode());

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
    Joshua Cranmer, Sep 30, 2008
    #3
  4. Vallabha

    Arne Vajhøj Guest

    Vallabha wrote:
    > I am trying to understand how BASE64Encoder() and BASE64Decoder()
    > work. I wrote a small sample for that:
    >
    > ==========================================
    >
    > import java.io.IOException;
    > import java.util.*;
    >
    > import sun.misc.BASE64Decoder;
    > import sun.misc.BASE64Encoder;
    >
    > public class Base64 {
    > public static void main(String[] args)throws IOException{
    > String a = new String("TEST");
    > System.out.println("Original string: " + a);
    > String encode = new sun.misc.BASE64Encoder().encode(a.getBytes());
    > System.out.println("Encoded String: " + encode);
    > byte[] decode = new sun.misc.BASE64Decoder().decodeBuffer(encode);
    > System.out.println("Decoded String: " + decode);
    > }
    > }
    >
    > ========================================
    > Original string: TEST
    > Encoded String: VEVTVA==
    > Decoded String: [B@addbf1
    > =========================================
    >
    > Here I was expecting the original string after the decoding. However
    > decoded string is different from the original one. Now how do I get
    > back my original string.


    Other have already pointed out the type issue.

    I will just add that:
    - I think you should use the Java EE javax.mail.internet classes
    instead of the sun.misc classes
    - I think you should consider an explicit encoding

    Arne
    Arne Vajhøj, Oct 4, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nut Cracker
    Replies:
    1
    Views:
    328
  2. jakk
    Replies:
    4
    Views:
    11,983
  3. Tim Meagher
    Replies:
    8
    Views:
    1,276
    Karl Seguin
    Jun 18, 2005
  4. Chris Berg

    BASE64Encoder

    Chris Berg, Aug 30, 2003, in forum: Java
    Replies:
    2
    Views:
    34,100
    karl wettin
    Sep 6, 2003
  5. titi

    if and and vs if and,and

    titi, Mar 9, 2007, in forum: VHDL
    Replies:
    4
    Views:
    561
    Mike Treseler
    Mar 11, 2007
Loading...

Share This Page