How to convert int/float etc into byte[]?  
Author Message
RC





PostPosted: 2007-8-25 3:13:00 Top

java-programmer, How to convert int/float etc into byte[]? For example

int i = 2;
char c = 'c';
float f = (float)3.45;
double d = 3.546;
String s = "Hello";

I want to write all these into a binary file.

In OutputStream, PrintStream classes
they have

write(int byte); and
write(byte[] buffer, int offset, int length);

My question is how do I convert those i,c,f,d,s into
byte[]?

Thank Q in advance!
 
Patricia Shanahan





PostPosted: 2007-8-25 3:28:00 Top

java-programmer >> How to convert int/float etc into byte[]? RC wrote:
> For example
>
> int i = 2;
> char c = 'c';
> float f = (float)3.45;
> double d = 3.546;
> String s = "Hello";
>
> I want to write all these into a binary file.
>
> In OutputStream, PrintStream classes
> they have
>
> write(int byte); and
> write(byte[] buffer, int offset, int length);

You can do it either the easy way, or the hard way.

The easy way is to wrap your OutputStream in a DataOutputStream and use
its writeInt, writeChar, writeFloat etc.

> My question is how do I convert those i,c,f,d,s into
> byte[]?

That would be the hard way. Do you still want to know?

Patricia
 
RC





PostPosted: 2007-8-25 3:41:00 Top

java-programmer >> How to convert int/float etc into byte[]? Patricia Shanahan wrote:
> RC wrote:
>> For example
>>
>> int i = 2;
>> char c = 'c';
>> float f = (float)3.45;
>> double d = 3.546;
>> String s = "Hello";
>>
>> I want to write all these into a binary file.
>>
>> In OutputStream, PrintStream classes
>> they have
>>
>> write(int byte); and
>> write(byte[] buffer, int offset, int length);
>
> You can do it either the easy way, or the hard way.
>
> The easy way is to wrap your OutputStream in a DataOutputStream and use
> its writeInt, writeChar, writeFloat etc.

Thank Q, I'll try that.

I tried this

Float ff = Float.valueOf((float)3.2);
byte b = ff.byteValue();

outputStream.write(b);

Is this make sense?

It is not make too much sense to me, because how do I know the
size of byte array (b)?

>> My question is how do I convert those i,c,f,d,s into
>> byte[]?
>
> That would be the hard way. Do you still want to know?
>
> Patricia
 
 
Roedy Green





PostPosted: 2007-8-25 5:56:00 Top

java-programmer >> How to convert int/float etc into byte[]? On Fri, 24 Aug 2007 15:13:19 -0400, RC <email***@***.com>
wrote, quoted or indirectly quoted someone who said :

>I want to write all these into a binary file.

there are a number of techniques.

1. DataOutputStream.
see http://mindprod.com/applet/fileio.html for sample code.

2. LEDataOutputStream (for little endian).
See http://mindprod.com/applet/fileio.html for sample code.

3. Nio. See http://mindprod.com/jgloss/nio.html

4. serialised objects.
see http://mindprod.com/jgloss/serialization.html

5. Double.doubleToLongBits.
see http://mindprod.com/jgloss/jgloss/floatingpoint.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
 
Roedy Green





PostPosted: 2007-8-25 5:57:00 Top

java-programmer >> How to convert int/float etc into byte[]? On Fri, 24 Aug 2007 15:40:51 -0400, RC <email***@***.com>
wrote, quoted or indirectly quoted someone who said :

>Float ff = Float.valueOf((float)3.2);
>byte b = ff.byteValue();
>
>outputStream.write(b);
no. You will end up with 3.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
 
Roedy Green





PostPosted: 2007-8-25 5:59:00 Top

java-programmer >> How to convert int/float etc into byte[]? On Fri, 24 Aug 2007 15:40:51 -0400, RC <email***@***.com>
wrote, quoted or indirectly quoted someone who said :

>
>Float ff = Float.valueOf((float)3.2);
>byte b = ff.byteValue();

For the tersest conversions see
http://mindprod.com/applet/converter.html

Unfortunately for you, even Java's tightest conversions from float to
byte will give you a byte, not the 32-bit IEEE bit representation of
the value.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
 
Ben Phillips





PostPosted: 2007-8-31 10:59:00 Top

java-programmer >> How to convert int/float etc into byte[]? Patricia Shanahan wrote:
> The easy way is to wrap your OutputStream in a DataOutputStream and use
> its writeInt, writeChar, writeFloat etc.
>
>> My question is how do I convert those i,c,f,d,s into
>> byte[]?
>
> That would be the hard way. Do you still want to know?

That is not the hard way; it is the not-quite-as-easy way.
DataOutputStream wrapping ByteArrayOutputStream.