Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: String and Bit Array conversion issues

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default String and Bit Array conversion issues

    Here is my code:

    byte[] temp1 = EMan.EncodeDataString(ExportStudent.FirstName);

    System.err.println("Original String");
    System.err.println(ExportStudent.FirstName);

    System.err.println("Original byte array");
    System.err.println(temp1);

    String STemp10 = temp1.toString();

    System.err.println("Transferred String");
    System.err.println(STemp10);

    byte[] temp23 = STemp10.getBytes();
    System.err.println("Transferred byte array");
    System.err.println(temp23);

    Here is my log:

    Original String
    Nelson
    Original byte array
    [B@2ac79769
    Transferred String
    [B@2ac79769
    Transferred byte array
    [B@15a58470

    Why is the conversion back to a Byte array screwing it up?


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: String and Bit Array conversion issues

    B@15a58470 etc is the result of printing an objects default toString() method.
    Note that calling toString() doesn't necessarily mean it prints out the byte array for you.

    For more information about toString(), ill refer you to this neat little article.
    Java toString Method | Java Beginner
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: String and Bit Array conversion issues

    From the toString method of Object in the Java API:

    The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

    getClass().getName() + '@' + Integer.toHexString(hashCode())


    Since arrays are object and do not have their own toString method they inherit the toString from Object. The above explains the output you are seeing.
    Improving the world one idiot at a time!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,647
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: String and Bit Array conversion issues

    Try using the Arrays toString() method for printing out the contents of arrays.

    The [B@15a58470 String is what is generated by the array object's to String method. It's saying there is an array ([) of bytes(B) at memory location: 15a58470

Similar Threads

  1. Char array to a String array with hex
    By fortune2k in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2014, 12:01 PM
  2. Conversion from multiple ints to single string
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 12th, 2011, 09:08 PM
  3. [SOLVED] Couldn't search for a string in an array.. Help please..
    By astrojunk in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2011, 09:47 PM
  4. RSA Decryption with Java.security - Hex to dec to byte array conversion...
    By SeanSeanston in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 15th, 2010, 08:48 AM
  5. [SOLVED] utf-16 byte[] to string conversion
    By Gerhardl in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 25th, 2010, 06:06 AM