Results 1 to 2 of 2
  1. #1
    7wb
    7wb is offline Member
    Join Date
    Nov 2010
    Posts
    1
    Rep Power
    0

    Default byte arrays and strings

    Hi everyone. My project asks that I store a byte array r into a text file, and then retrieve it again later, so that would mean I would have to convert a byte array into a string, and then back to a byte array. The professor said to use java.util.Arrays.toString. Right now I'm just testing out to see how the conversion works:

    byte[] r = new byte[] {10, 20, 30, 40, 50, 60};
    String r2 = java.util.Arrays.toString(r);
    System.out.println("Before: " + r2);
    byte[] r3 = r2.getBytes();
    System.out.println("After: " + java.util.Arrays.toString(r3));

    And I'm getting:

    Before: [10, 20, 30, 40, 50, 60]
    After: [91, 49, 48, 44, 32, 50, 48, 44, 32, 51, 48, 44, 32, 52, 48, 44, 32, 53, 48, 44, 32, 54, 48, 93]

    Which are definitely not the same... what am I doing wrong? :confused:

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Handling Multi byte Character Strings
    By madhav1351 in forum Advanced Java
    Replies: 2
    Last Post: 09-22-2010, 01:55 PM
  2. write/read two byte arrays to one file- help!
    By leshep in forum New To Java
    Replies: 2
    Last Post: 11-27-2009, 11:10 AM
  3. Byte arrays and MIDI - simple question?
    By Ravaa in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 10:47 PM
  4. using Byte arrays
    By mew in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 04:54 AM
  5. arrays strings and doubles
    By rgvbabe in forum New To Java
    Replies: 1
    Last Post: 01-14-2008, 12:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •