Results 1 to 13 of 13

Thread: Byte Array

  1. #1
    sandor is offline Member
    Join Date
    Apr 2007
    Location
    Pennsylvania,USA
    Posts
    45
    Rep Power
    0

    Default Byte Array

    I have a PublicKey object that I haved called getEncoded() on to get its representation as an array of bytes.
    I then want to send these to another client so they can use the array of bytes.
    InputStreamReader can only read Strings at a time, so I have the right value but in the form of a String.
    I need to convert the value I have in my String to bytes. Example:

    I print out the array of bytes on the server side, I get:

    [B@c79809

    I print out the String read on the receiving end, I get:

    [B@c79809

    It's the same, so I want that exact value in an array of bytes rather than a String. How do I get that?

  2. #2
    derrickD is offline Member
    Join Date
    Apr 2007
    Location
    USA
    Posts
    50
    Rep Power
    0

    Default

    The subclasses of Reader are meant for character streams.

    To read bytes, look at BufferedInputStream.

    Just a reminder that what you see in "[B@c79809" is a string representation of a byte array; the "[B" part signifies that it's a byte array
    (as opposed to, say, an int array), and the "@c79809" part is a representation of the array's address. You may know this already,
    but I wanted to make sure you didn't mistake that string for the encoded byte values.

  3. #3
    peiceonly is offline Member
    Join Date
    Mar 2007
    Posts
    41
    Rep Power
    0

    Default

    If the "toString () " method is not implemented we get the values something like this ? So the question is what is the relationship between bytearray and the toString ?

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Use Base64 encoding, I found one in the libs yesterday and as well have an open source Base64 encoder.

    Since we have key material or something, doing toString() and so on is either inefficient, ineffective or insecure for some reason or another. The proof of concept is that any and all pro's use Base64 encoding for transmission of binary - that is what it was designed for and would not have been written unless there was a need for it.

  5. #5
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Free open source code for this at my website, pfarrell.com
    look in the /java subdirectory
    Technical Notes

  6. #6
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    I think the following code will give your some figures:

    byte b1[] = new byte[]{1, 11, 2, 22, 33, 127};
    System.out.println(Arrays.toString(b1));

    String s = new String(b1, "utf-8");
    //System.out.println(s);

    byte b2[] = s.getBytes("utf-8");
    System.out.println(Arrays.toString(b2));

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default

    I have the right value but in the form of a String. .
    How did you convert the byte[] to String? That will determine how to
    convert the value I have in my String to bytes

  8. #8
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Exclamation base64 v DataOutputStream

    There is a missing element here of how it is that original poster is stuck with going through a string, I noticed in ftr's comments in the base64 encoder something to the effect of may not be reversable. As Norm states, it is how opie converts in the first place that matters for getting data back to bin - hex - octets but going through String method seems to introduce indefinite risks surrounding contemporary String ( ususaly viewed as human-readable or at least containing few if any nulls ) vis-a-vis byte / binary data transmission. Those are distinguishable problem domains.

    {Norm, I got a couple of 500-800 page Java books back from my bench tech, let me know if you want me to ship them to you as they are not being used by me any more.}
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  9. #9
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    I agree that going though String is potentially dangerous, but...There is no Base64 encoder/decoder in the standard Java SDK class library !

    There is Sun specific sun.misc.BASE64Encoder, but it is undocumented and generally can not be used.

    You can use things like this:
    Base64: Public Domain Base64 Encoder/Decoder
    Last edited by ProjectKaiser; 08-29-2008 at 02:59 PM.

  10. #10
    kHim is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default

    heLLo,,!!
    cAn u pLeAse give mE tHE cOde of tHis oUtpUt?
    using aRRay

    @
    @@
    @ @
    @@@@
    @ @
    @@ @@
    @ @ @ @
    @@@@@@@@
    @ @
    @@ @@
    @ @ @ @
    @@@@ @@@@
    @ @ @ @
    @@ @@ @@ @@
    @ @ @ @ @ @ @ @
    @@@@@@@@@@@@@@@@
    @ @
    @@ @@
    @ @ @ @
    @@@@ @@@@
    @ @ @ @
    @@ @@ @@ @@
    @ @ @ @ @ @ @ @
    @@@@@@@@ @@@@@@@@
    @ @ @ @
    @@ @@ @@ @@
    @ @ @ @ @ @ @ @
    @@@@ @@@@ @@@@ @@@@
    @ @ @ @ @ @ @ @
    @@ @@ @@ @@ @@ @@ @@ @@
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

  11. #11
    kHim is offline Member
    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Default arrays

    heLLo,,!!
    cAn u pLeAse give mE tHE cOde of tHis oUtpUt using two dimensional array?
    thAnks a lot

    @
    @@
    @ @
    @@@@
    @ @
    @@ @@
    @ @ @ @
    @@@@@@@@
    @ @
    @@ @@
    @ @ @ @
    @@@@ @@@@
    @ @ @ @
    @@ @@ @@ @@
    @ @ @ @ @ @ @ @
    @@@@@@@@@@@@@@@@
    @ @
    @@ @@
    @ @ @ @
    @@@@ @@@@
    @ @ @ @
    @@ @@ @@ @@
    @ @ @ @ @ @ @ @
    @@@@@@@@ @@@@@@@@
    @ @ @ @
    @@ @@ @@ @@
    @ @ @ @ @ @ @ @
    @@@@ @@@@ @@@@ @@@@
    @ @ @ @ @ @ @ @
    @@ @@ @@ @@ @@ @@ @@ @@
    @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

  12. #12
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default Additional query with the same problem

    When I am running the code as follows:
    BufferedReader userInput = new BufferedReader (new InputStreamReader(System.in));
    System.out.println("Enter String: ");
    String input = userInput.readLine();
    System.out.println("UserInput:"+input);
    byte[] sha1hash = input.getBytes();
    System.out.println("ByteFormat:"+sha1hash);
    String t = new String( sha1hash);
    System.out.println("StringFormat:"+t);
    for (int i = 0; i < sha1hash.length; i++)
    System.out.println("ByteArray["+i+"]:"+sha1hash[i]);

    I am getting the output as follows:
    Enter String:
    23
    UserInput:23
    ByteFormat:[B@10b62c9
    StringFormat:23
    ByteArray[0]:50
    ByteArray[1]:51

    I am unable to understand that when I am printing each element of the byte array where from it is getting those values.

  13. #13
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    You're not doing a hash, it is something else.....
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Byte Values
    By javaplus in forum New To Java
    Replies: 1
    Last Post: 06-23-2008, 12:08 AM
  2. using Byte arrays
    By mew in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 03:54 AM
  3. BufferedImage to Byte
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-22-2008, 08:17 PM
  4. Reading/Writing a File using byte array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 10:41 AM
  5. int to byte
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 07:22 PM

Posting Permissions

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