🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.java.programmer
Article #327

Re: Int / Short to byte[] and back

#327
From: Bill Wilkinson
Date: Thu, 14 May 1998 00:00
126 lines
4925 bytes
ted wrote:
> 
> Does anyone have some code to do the following functions?
> 
> public static byte[] convert(int i) {}
> public static byte[] convert(short i) {}
> 
> public static int convert(byte[] i) {}
> public static short convert(byte[] i) {}
> 
> Please comment on whether your code does:
>     i = 65450; //FFAA
> 
>     byte[0] = 0xFF; byte[1]=0xAA;
>         or
>     byte[0] = 0xAA; byte[1]=0xFF;
> 
> Which is the internal representation in java and is this 
> called big or little edian?

You have a misconception here.  The INTERNAL representation is
never specified for Java.  It wouldn't make sense for it to be.
Java uses big-endian for EXTERNAL representation, but imagine 
how slowly Java code would execute on an Intel (x86, which is
little-endian) platform if every time some operation fetched a 
value from memory the bytes had to be swapped, the operation 
performed, the bytes reswapped, and then the result stored in memory.
I shudder to think of it.

That being said, let's talk EXTERNAL representation.

If you have
	short s = 65450;
then you can do
	System.out.println( s == 0xFFAA );
and expect "true" to be printed on ANY machine.  That's 
because BOTH operands of the "==" are "converted" to int
values before the comparison, and then that comparison
takes place in the internal representation of the machine, 
in a way completely transparent to you.  (Theoretically, you
could run Java on a machine that only worked with 36 bit values,
shades of the old CDC machines; so long as the conversions AT
THE EXTERNAL level were done correctly, you shouldn't be able
to detect what was happening internally.)

If you do
	FileOutputStream fos = new FileOutputStream("test.data");
	DataOutputStream dos = new DataOutputStream( fos );
	dos.writeShort( 65450 );
and then dump the bytes of that "test.data" file, you'll find
that the bytes, in order, are
	FF AA
and this is called "big endian" and is the standard EXTERNAL
representation for Java, no matter what internal representation
is used!  (COMMENT:  If you NEED to communicate with a non-Java
program that expects little endian data, then look at Roedy 
Green's "Java Glossary" Web site and find LEDataOutputStream and
LEDataInputStream.)

Anyway, to answer your original question, LOTS of people
have written the conversions you ask about.  They are, 
obviously, fairly trivial (once you get stuck in your head
that Java always does operations as "int" or "long"...see
my other reply to your other question).  

Examples:
	public static byte[] convert(int i)
	{
		byte[] b = new byte[4];
		for ( int j = 3; j >= 0; --j )  // big endian
//		for ( int j = 0; j < 4; ++j )   // little endian
		{
			b[j] = (byte) i; // grab lower 8 bits
			i >>> 8;
		}
		return b;
	}

	public static int convert( byte[] b )
		throws IllegalArgumentException
	{
		if ( b.length < 4 ) throw new IllegalArgumentException;	
		int i = 0;
		for ( int j = 0; j < 4; ++j )  // big endian
//		for ( int j = 3; j >= 0; --j ) // little endian
		{
			i = ( i << 8 ) | ( b[j] & 0xff );
				// the "& 0xff" is IMPORTANT!
		}
		return i;
	}

There are, of course, other ways to do all that.

There is even a built-in way in the system, though (a) it
isn't obvious and (b) it's really designed for converting
lots of data, rather than a single int or short at a time.
The scheme:

	ByteArrayOutputStream bos = new ByteArrayOutputStream( );
	DataOutputStream dos = new DataOutputStream( bos );
	dos.writeInt( 71151230 );
	dos.writeDouble( Math.PI );
	dos.flush( );
	byte[] converted = bos.toByteArray( );
	for ( int i = 0; i < converted.length; ++i )
		System.out.println("byte "+i+" is "+converted[i]);

Incidentally, that little code sequence should print out 12 byte
values (4 from the int and 8 from the double).

As you might expect, you can use a ByteArrayInputStream to go
the other way.  But I'll leave the playing with that to you.

And note you can also use Roedy's LEDataxxxStream classes with
the ByteArrayxxxStream classes, also, if you need little endian
conversions.

Have fun.

**********************

Bill Wilkinson, SuperCede, Inc.

(Hi, Borland people!)
Technical Details
Message-ID: <355B48F9.3719@SuperCede.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <355B0094.D9D67382@hotmail.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10a146,547ec6700db86f3d
X-Google-Attributes: gid10a146,public
X-Google-Thread: 1089d0,547ec6700db86f3d
X-Google-Attributes: gid1089d0,public
From: Bill Wilkinson <BillW@SuperCede.com>
Subject: Re: Int / Short to byte[] and back
Date: 1998/05/14
Message-ID: <355B48F9.3719@SuperCede.com>
X-Deja-AN: 353291445
Content-Transfer-Encoding: 7bit
References: <355B0094.D9D67382@hotmail.com>
To: ted <tedted40@hotmail.com>
Content-Type: text/plain; charset=us-ascii
Organization: SuperCede, Inc.
Mime-Version: 1.0
Reply-To: BillW@SuperCede.com
Newsgroups: comp.lang.java.programmer,borland.public.jbuilder.java.api