byte[] > string > byte[] problem

Discussion in 'Java' started by Peter, Aug 5, 2004.

  1. Peter

    Peter Guest

    Hi
    byte b[] = new byte[3];
    b[0] = -1;
    b[1] = -2;
    b[2] = 63;
    String sss="";
    try {
    sss=new String(b, "ISO8859_1");
    } catch (Exception ex) {
    }
    byte c[] = sss.getBytes();
    for (int x = 0; x < c.length; x++) {
    System.out.println( (int) c[x]);
    }


    it print out
    63
    63
    63

    i suppose it print
    -1
    -2
    63.
    Please tell me how to fix it.
    thanks
    from Peter()
    Peter, Aug 5, 2004
    #1
    1. Advertising

  2. Peter

    zoopy Guest

    On 5-8-2004 12:13, Peter wrote:

    > Hi
    > byte b[] = new byte[3];
    > b[0] = -1;
    > b[1] = -2;
    > b[2] = 63;
    > String sss="";
    > try {
    > sss=new String(b, "ISO8859_1");
    > } catch (Exception ex) {
    > }
    > byte c[] = sss.getBytes();
    > for (int x = 0; x < c.length; x++) {
    > System.out.println( (int) c[x]);
    > }
    >
    >
    > it print out
    > 63
    > 63
    > 63
    >
    > i suppose it print
    > -1
    > -2
    > 63.
    > Please tell me how to fix it.
    > thanks
    > from Peter()


    Well, on J2SE 1.4.2 (Win2000) it *does* print -1 -2 63.

    Which Java/platform are you using?

    You shouldn't catch exceptions silently. Try
    catch (Exception ex) {
    ex.printStackTrace();
    }
    and see if there is an error.

    Regards,
    Z.
    zoopy, Aug 5, 2004
    #2
    1. Advertising

  3. Peter wrote:

    > sss=new String(b, "ISO8859_1");
    > byte c[] = sss.getBytes();


    In the second line, you don't specify a charset, so the
    platform default is used, and tha is probably US-ASCII, which
    cannot represent the characters corresponding (in Latin1) to
    the Java byte values of -1 (y umlaut) and -2 (thorn). Therefore
    the character 63 (question mark) is inserted.

    The real question is why would you want to do this anyway? Looks
    to me like an attempt to encode binary data in string. A very
    bad idea, but if you really cannot avoid it, use a base64 or
    hex encoding.
    Michael Borgwardt, Aug 5, 2004
    #3
  4. zoopy wrote:
    > Well, on J2SE 1.4.2 (Win2000) it *does* print -1 -2 63.


    Only if your platform default encoding is ISO8859-1.
    Michael Borgwardt, Aug 5, 2004
    #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. Andreas
    Replies:
    1
    Views:
    833
    Jonathan Bromley
    May 4, 2004
  2. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,214
    Roedy Green
    Aug 5, 2003
  3. Jean-Daniel Gamache
    Replies:
    0
    Views:
    415
    Jean-Daniel Gamache
    Jul 14, 2004
  4. jt
    Replies:
    3
    Views:
    915
    Keith Thompson
    May 23, 2005
  5. Polaris431
    Replies:
    8
    Views:
    741
    SM Ryan
    Dec 4, 2006
Loading...

Share This Page