how to cast a primitive type 'long' to byte[8]

F

Ferenc Hechler

I don't think it can be done by a simple cast,
because the byte represantation of a long is system
dependant (little-endian, big-endian).
 
T

Tony Morris

stingar said:

// Every once in a while, I spoon-feed.
// Next time, use the Java API Specification like I just did.

import java.io.*;

public class X
{
public static void main(String[] args) throws IOException
{
long l = 314159265L;

ByteArrayOutputStream baos = new ByteArrayOutputStream(8);
DataOutputStream dos = new DataOutputStream(baos);
dos.writeLong(l);
byte[] bytes = baos.toByteArray();

for(byte b : bytes)
{
System.out.println(b);
}
}
}
 
A

Andrei Kouznetsov

import java.io.*;
public class X
{
public static void main(String[] args) throws IOException
{
long l = 314159265L;

ByteArrayOutputStream baos = new ByteArrayOutputStream(8);
DataOutputStream dos = new DataOutputStream(baos);
dos.writeLong(l);
byte[] bytes = baos.toByteArray();

for(byte b : bytes)
{
System.out.println(b);
}

using uio (http://uio.dev.java.net) you can direct write and read any
primitive data type.
 
B

Boudewijn Dijkstra

stingar said:

byte[] ba = new byte[8];

big-endian:

ba[7] = (byte) j;
ba[6] = (byte) (j >>= 8);
ba[5] = (byte) (j >>= 8);
ba[4] = (byte) (j >>= 8);
ba[3] = (byte) (j >>= 8);
ba[2] = (byte) (j >>= 8);
ba[1] = (byte) (j >>= 8);
ba[0] = (byte) (j >> 8);

little-endian:

ba[0] = (byte) j;
ba[1] = (byte) (j >>= 8);
ba[2] = (byte) (j >>= 8);
ba[3] = (byte) (j >>= 8);
ba[4] = (byte) (j >>= 8);
ba[5] = (byte) (j >>= 8);
ba[6] = (byte) (j >>= 8);
ba[7] = (byte) (j >> 8);
 
X

xarax

Ferenc Hechler said:
I don't think it can be done by a simple cast,
because the byte represantation of a long is system
dependant (little-endian, big-endian).

Totally wrong on the Endian thing. Java is
ALWAYS big endian.
 
C

Chris Smith

xarax said:
Totally wrong on the Endian thing. Java is
ALWAYS big endian.

I'd put that differently. Java standard API methods that convert data
to and from bytes always operate either in big-endian (for example,
DataOutputStream) or give you a choice (for example, ByteBuffer views).
Internally, the JVM is free to use any byte ordering it likes, because
the language spec is designed to make it impossible to tell the
difference. I'd expect that internally, JVMs on little-endian platforms
would represent numbers as little-endian internally.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

xarax said:
Totally wrong on the Endian thing. Java is
ALWAYS big endian.

Not true. The java platform is endian agnostic. It is true that for
example the class file format uses big endian representation of data,
but this is simply because you can't have a portable transmission format
if you don't specify endianess. Beyond that, the JVM is free to use
whatever endianess the target platform uses. I can guarantee you that on
an x86 and any reasonable JVM, an int is little endian.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top