Results 1 to 3 of 3
  1. #1
    kronox is offline Member
    Join Date
    Nov 2011
    Rep Power

    Default Byte[] to string and string to byte[] - for database

    Ok, so Ive looked and looked for an appropriate answer for this with no avail.
    Hopefully I can find something soon.


    I am working with a PHP Java bridge, so all my database access is done through PHP and heavy work loads are done through Java.

    I have a list of numbers, which I create a byte[] array of, then pass the string representation back to PHP to store in the database (of which I am using BLOB) ... the string representations of the byte[] array looks like [B@6ba80b

    I then get the value '[B@6ba80b' from the database in a string format to pass back to Java .. in Java however, I have the string value and cannot figure out how to get this back into a byte[] array.

    So far my efforts result in a different byte[] array than what I need.

    I get:
    from database to converted to byte[] with string.getBytes()
    [B@12bfb8d [B@1a62d05
    [B@15b588f [B@62b9dd

    as you can see, these are not the same.

    Any ideas would be great!!


  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power

    Default Re: Byte[] to string and string to byte[] - for database

    [B@xxxxx where x are hex numbers is the default toString() returned by a byte array, and that's not what you want. For a String representation of the byte array, you'd use new String(myByteArray);. But I think we need to see more of your code to figure out exactly what you're doing wrong. Especially how you try to pass the byte array to PHP.

  3. #3
    kronox is offline Member
    Join Date
    Nov 2011
    Rep Power

    Default Re: Byte[] to string and string to byte[] - for database

    Its not so much of a worry how its passed to PHP for this part
    Initially the code is a List<Integer> which is encoded into a byte[] array
    A file is created with lines to insert into the database using in file ... etc

    line = line.trim();
    String[] parts = line.split("=");
    SortedMap<Integer, Integer> docid_freq_map = Index.getDocIdMap(parts[1]);
    List<Integer> delta_freq_list = Index.sortedmap2deltalist(docid_freq_map);
    int df = docid_freq_map.size();
    byte[] bytes = VB.VBENCODE(delta_freq_list);
    String contents = parts[0] + "||" + df + "||" + bytes + "\n";

    the file looks like: (delimeted by ||)



    From PHP I send a String which looks exactly like each line from above (yanke||1||[B@1845a6)
    I split the String into:
    String word = "yanke"
    String misc = 1
    String sBytes = "[B@1845a6"

    so, what I need to do, is essentially get that sBytes back into a byte[] bytes array with exactly the same value as it was previously before inserting into the database

Similar Threads

  1. Expressing a byte string
    By DeptOfMeteors in forum New To Java
    Replies: 14
    Last Post: 11-25-2010, 10:47 PM
  2. Converting string to byte[]
    By bobo67 in forum New To Java
    Replies: 12
    Last Post: 09-10-2010, 09:10 PM
  3. String byte array
    By myka in forum Advanced Java
    Replies: 2
    Last Post: 03-18-2010, 02:33 AM
  4. String from byte array
    By justint in forum New To Java
    Replies: 2
    Last Post: 01-22-2010, 06:58 AM
  5. String byte storage
    By bozovilla in forum New To Java
    Replies: 1
    Last Post: 11-24-2007, 06:35 AM

Tags for this Thread

Posting Permissions

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