Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Double into byte...can you help me,please?I'm crazing...

Reply
Thread Tools

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

 
 
gbattine
Guest
Posts: n/a
 
      06-15-2006
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...............

 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      06-16-2006
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


 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      06-16-2006

"gbattine" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
replace double inverted commas into slash double inverted commas in string AviraM Java 2 09-28-2006 06:19 PM
from List <double> to double[] Web learner ASP .Net 3 04-26-2006 05:26 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Double double display display problem problem Tom Accuosti Firefox 3 09-27-2004 10:02 PM
Help: Double convert to Integer and Double.... da Vinci C++ 5 07-31-2004 12:35 AM



Advertisments