Results 1 to 13 of 13
  1. #1
    bobo67 is offline Member
    Join Date
    Sep 2010
    Posts
    37
    Rep Power
    0

    Question Converting string to byte[]

    hi,
    how to convert String to byte[]?

    My code:
    //Converting byte[] to String
    byte [] M1e = ConcatBytes(a1e,a2e,a3e);
    String msg = new String(M1e);

    //converting String to byte[]
    byte[] Me = msg.getBytes();

    but Me is not M1e!!:confused:

    Regards,

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by bobo67 View Post
    hi,
    how to convert String to byte[]?

    My code:
    //Converting byte[] to String
    byte [] M1e = ConcatBytes(a1e,a2e,a3e);
    String msg = new String(M1e);

    //converting String to byte[]
    byte[] Me = msg.getBytes();

    but Me is not M1e!!:confused:

    Regards,
    It all depends on the encoding and decoding used; there's also a getBytes() method that allows you to specify the encoding. I don't know what that ConcatBytes( ... ) method does but the encoding and decoding have to match.

    kind regards,

    Jos

  3. #3
    bobo67 is offline Member
    Join Date
    Sep 2010
    Posts
    37
    Rep Power
    0

    Unhappy

    It just concatenate three byte array.here's the code:

    private static byte[] ConcatBytes(byte[] a1e, byte[] a2e, byte[] a3e) {
    byte concatenatedBytes[] = new byte[a1e.length + a2e.length + a3e.length];
    for (int i=0; i<a1e.length; i++)
    concatenatedBytes[i] = a1e[i];
    for (int j=0; j<a2e.length; j++)
    concatenatedBytes[j+a1e.length] = a2e[j];
    for (int j=0; j<a3e.length; j++)
    concatenatedBytes[j+a1e.length+a2e.length] = a3e[j];
    return concatenatedBytes;
    }
    I convert Byte[] M1e to String and then convert it to Byte[] again, but the result isn't equal with M1e.
    What I have to do?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by bobo67 View Post
    It just concatenate three byte array.here's the code:

    private static byte[] ConcatBytes(byte[] a1e, byte[] a2e, byte[] a3e) {
    byte concatenatedBytes[] = new byte[a1e.length + a2e.length + a3e.length];
    for (int i=0; i<a1e.length; i++)
    concatenatedBytes[i] = a1e[i];
    for (int j=0; j<a2e.length; j++)
    concatenatedBytes[j+a1e.length] = a2e[j];
    for (int j=0; j<a3e.length; j++)
    concatenatedBytes[j+a1e.length+a2e.length] = a3e[j];
    return concatenatedBytes;
    }
    I convert Byte[] M1e to String and then convert it to Byte[] again, but the result isn't equal with M1e.
    What I have to do?
    I'd say don't convert your byte[] to a String and back again. The conversion to a String (internally a char[]) decodes the byte array and when you convert that String back again to a byte[] probably another (non-matching) encoding is used. If you insist in doing byte[] --> String --> byte[] both then encoding and decoding algorithms must match. Read the API documentation for the String class; it has methods that allow you to specify the en/decoding; utf-8 is a good candidate.

    kind regards,

    Jos

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    I found that UTF-8 was symmetrical(what goes in comes out) up to 0X7F.
    ISO-8859-1 was symmetrical past 0x7F

    Java Code:
    // Test  if String can hold 256 different values from a byte array
    
    import java.nio.charset.*;
    
    public class TestByteToString {
    
       public static void main(String args[]) {
         byte[] all256 = new byte[256];
         // fill array with 0-255
         for(int i=0; i < 256; i++) {
           all256[i] = (byte)i;
         }
    
         Charset charset = Charset.forName("ISO-8859-1");      //<<< this gives 100% equality
    
         String aStr = new String(all256, charset);
         int cnt = 0;
         for(int i=0; i < 256; i++) {
           if(i != aStr.charAt(i)) {
             System.out.println("not equal i=" + i + ", char=" + Integer.toHexString(aStr.charAt(i)));
           }else{
             cnt++;
           }
         } // end for(i)
         System.out.println("matched OK for cnt=" + cnt);
      }
    }
    Last edited by Norm; 09-10-2010 at 02:42 PM.

  6. #6
    bobo67 is offline Member
    Join Date
    Sep 2010
    Posts
    37
    Rep Power
    0

    Unhappy

    thank u, but my array size is 384...
    I also use utf-8, but it doesn't work right.

    byte[] M1e = ...;
    String msg = new String(M1e , "utf-8");
    System.out.println(msg.getBytes("utf-8"));

    I have a byte array in the client side, and want to send it to server..That's why I'm converting it to string and at the server side I have to convert it to byte array again..

    regards,

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by bobo67 View Post
    I have a byte array in the client side, and want to send it to server..That's why I'm converting it to string and at the server side I have to convert it to byte array again..
    If you want to send a byte array and the other side wants to receive a byte array, by all means just send the byte array and leave out all that conversion stuff.

    kind regards,

    Jos

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    but my array size is 384...
    Not relevant. Its the contents that matters. My array has 256 elements that hold the values from 0 to 255. My test was to see if what went in (values 0-255) came out with the same value. It did for ISO-8859-1. It does NOT using UTF-8 for bytes > 0x7F

    What is the range of values in your String? Are any higher than 0XFF in value? If they are, then the number of bytes required to hold the String will be greater than the String length.

  9. #9
    bobo67 is offline Member
    Join Date
    Sep 2010
    Posts
    37
    Rep Power
    0

    Question

    Quote Originally Posted by JosAH View Post
    If you want to send a byte array and the other side wants to receive a byte array...
    Jos
    I sent byte array but in the sever I receive string..
    how to receive byte??
    here's my server code:

    String x = "";
    ServerSocket srvr = new ServerSocket(4444);
    Socket skt = srvr.accept();
    System.out.print("Server has connected!\n");
    BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
    while (!in.ready()) {}
    x = in.readLine();

    Regards,

  10. #10
    bobo67 is offline Member
    Join Date
    Sep 2010
    Posts
    37
    Rep Power
    0

    Question

    Quote Originally Posted by Norm View Post
    What is the range of values in your String? Are any higher than 0XFF in value?
    I don't know the values because my array is the output of my encryption function.
    here's my encryption function:

    public byte[] RSAEncrypt(byte[] data ,String filename) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
    PublicKey pubKey = readKeyFromFile("pub" + filename + ".key");
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, pubKey);
    byte[] cipherData = cipher.doFinal(data);
    return cipherData;
    }

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by bobo67 View Post
    I sent byte array but in the sever I receive string..
    how to receive byte??
    here's my server code:

    String x = "";
    ServerSocket srvr = new ServerSocket(4444);
    Socket skt = srvr.accept();
    System.out.print("Server has connected!\n");
    BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
    while (!in.ready()) {}
    x = in.readLine();

    Regards,
    That is quite a silly server you have there: it wants you to send bytes but it can only receive characters; what kind of server is that?

    kind regards,

    Jos

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    I don't know the values because my array ...
    If you look at the type of the array(byte[] cipherData), you will see it is byte. The max value of a byte is 255.

  13. #13
    bobo67 is offline Member
    Join Date
    Sep 2010
    Posts
    37
    Rep Power
    0

Similar Threads

  1. converting byte array to bmp file
    By Moorkh in forum New To Java
    Replies: 2
    Last Post: 09-07-2010, 03:58 PM
  2. Need help converting int to a 4 byte array
    By kook04 in forum Advanced Java
    Replies: 5
    Last Post: 02-26-2010, 09:59 PM
  3. Converting Image to byte array[] ?
    By afflictedd2 in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-12-2009, 12:33 AM
  4. Converting a byte[] into Sound
    By savage82 in forum Advanced Java
    Replies: 1
    Last Post: 11-22-2007, 12:57 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
  •