Double into byte...can you help me,please?I'm crazing...

Discussion in 'Java' started by gbattine, Jun 15, 2006.

  1. gbattine

    gbattine Guest

    Hi guys,
    it's the first time i have to solve these conversions and i'm having
    too problems......
    Excuse me for my english and my java inexperience,i need your help fpr
    two situations:

    1)How can i convert a string into an array of byte?
    2) How can i convert an array of double into an array of byte?

    What i care in the second problem is converting the array of double
    according to a fixed format,like adding & beetwen two converted values
    for giving me the possibility to rebuild original format of array
    later.
    Can you give me some code and comments to understand something?
    I'm crazing and java documentation don't help me...............
    gbattine, Jun 15, 2006
    #1
    1. Advertising

  2. gbattine

    Chris Uppal Guest

    gbattine wrote:

    > 1)How can i convert a string into an array of byte?


    Normally you would use some fixed char encoding. E.g.

    byte[] bytes = aString.getBytes("UTF-8");


    > 2) How can i convert an array of double into an array of byte?


    One way would be to use a DataOutputStream:

    ByteArrayOutputStream bytestream =
    new ByteArrayOutputStream();
    DataOutputStream datastream =
    new DataOutputStream(bytestream);
    for (double d : aDoubleArray)
    datastream.writeDouble(d);
    datastream.flush();
    byte[] bytes = bytestream.toByteArrray();

    which will dump each double as it's raw 8-byte representation to the stream.
    Since each double takes exactly 8-bytes regardless of its magnitude, you
    probably don't need to include separators in the stream. If you want them
    anyway, then you can add them usng the other methods of DataOutputStream.

    Alternatively you can use Double.doubleToLongBits() to convert a double into a
    64-bit long with the same bit pattern, and then use bitmanipulation to extract
    the 8 individual bytes.

    -- chris
    Chris Uppal, Jun 16, 2006
    #2
    1. Advertising

  3. gbattine

    Oliver Wong Guest

    "gbattine" <> wrote in message
    news:...
    > Hi guys,
    > it's the first time i have to solve these conversions and i'm having
    > too problems......
    > Excuse me for my english and my java inexperience,i need your help fpr
    > two situations:
    >
    > 1)How can i convert a string into an array of byte?
    > 2) How can i convert an array of double into an array of byte?
    >
    > What i care in the second problem is converting the array of double
    > according to a fixed format,like adding & beetwen two converted values
    > for giving me the possibility to rebuild original format of array
    > later.


    Note that '&' is not a byte, but a character. It doesn't make sense to
    add a '&' to a byte stream; instead, you'd add the ASCII byte-representation
    of the '&' character, or the UTF-16LE byte-representation of the '&' (which
    is different from the ASCII representation).

    When designing your format, if you're going to have seperators, you have
    to make sure that you can distinguish between the seperators and the values.

    An alternative is to use fixed-length value encodings. For example,
    always store all your double values as 4 bytes. Then you know where each
    double starts or ends, without the use of seperators.

    - Oliver
    Oliver Wong, Jun 16, 2006
    #3
    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. KK
    Replies:
    2
    Views:
    546
    Big Brian
    Oct 14, 2003
  2. Sydex
    Replies:
    12
    Views:
    6,473
    Victor Bazarov
    Feb 17, 2005
  3. DaBeef
    Replies:
    1
    Views:
    614
    Matt Humphrey
    Jul 21, 2006
  4. AviraM
    Replies:
    2
    Views:
    6,355
    Manish Pandit
    Sep 28, 2006
  5. jeko

    reading a double var byte per byte

    jeko, Jan 18, 2005, in forum: C Programming
    Replies:
    12
    Views:
    547
Loading...

Share This Page