By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,389 Members | 2,065 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,389 IT Pros & Developers. It's quick & easy.

Converting arrays into byte arrays

P: n/a
Hi there,
I am trying to write a method that accepts an array of any primitive
type and will return the same array without copying memory as an array
of bytes.

ie. I'd like to be able to do something like:

char[] chars = "Hello!";
byte[] bytes = (byte[]) chars;

which obviously won't work.

Is there any way of casting arrays?

Thanks for any help you can give me.

Matt
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Wed, 06 Aug 2003 11:36:59 +0100, matt melton <ha***********@yahoo.co.uk>
two-finger typed:
Hi there,
I am trying to write a method that accepts an array of any primitive
type and will return the same array without copying memory as an array
of bytes.
You will have to overload the method (i.e. write a different method for
each type of array).

ie. I'd like to be able to do something like:

char[] chars = "Hello!";
You are assigning a String object to an array ?
Use:
String hello = "Hello!" ;
char [] chars = new char [hello.length()];
hello.getChars(0,hello.length(),chars,0);

Or:
char [] chars = getChars("Hello!");

with an extra (local) method:
public char [] getChars(String s) {
char [] chars = new char [s.length()];
hello.getChars(0,s.length(),chars,0);
}
byte[] bytes = (byte[]) chars;
bytes and chars are two different things.
chars are 16 bit (Unicode), bytes are 8 bit.

The resulting bytes out of a string would have to represent an encoding of
the string to support Unicode characters above the codevalue 255.

Have a look at the String API for more information.

Converting doubles and floats into bytes may pose even more of a challenge,
if you want the storage to be according to IEEE standards.
Have you looked at Serialization ?
which obviously won't work.

Is there any way of casting arrays?

Thanks for any help you can give me.

Matt

Cheers.
Jul 17 '05 #2

P: n/a

"matt melton" <ha***********@yahoo.co.uk> wrote in message news:3F***************@yahoo.co.uk...
Hi there,
I am trying to write a method that accepts an array of any primitive
type and will return the same array without copying memory as an array
of bytes.

ie. I'd like to be able to do something like:

char[] chars = "Hello!";
byte[] bytes = (byte[]) chars;

which obviously won't work.

Is there any way of casting arrays?


Actually, you don't need to 'cast' arrays of primitives to arrays of bytes.

What you need is something else (I don't know what, exactly), but you
think, for some reason, that your goal can be acomplished by doing
something like that (treating a block of memory like an array of bytes,
regardless of what's inside that block. That's precisely what you shouldn't
even think about, in Java. Go back to C for stunts like that. (-: )

So, it would be better if you went one step back and said what you really
wanted to do, instead of asking detailed help on something that could
easily be a bad idea in the first place.

Jul 17 '05 #3

P: n/a
Hi

I might need to elaborate on what I'm really trying to do,

I am writing an API to allow simple message passing of arrays between
VM's on different machines. I'd like to use UDP and Datagrams for the
trasfer ( I have my reasons, they may not be valid though ).
I want it to be as light weight and quick as possible:
So just a simple byte tag to identify the type of the array, rather than
serializing the array.
As little memory copying between the array itself and a DatagramPacket
as possible, the minimum being zero if it is feasible ( which I am
beginning to think isn't).

What I really want is to be able to view a primitive array,
as an array of any other primitive type.

ie

byte[16] bytearray int[4] intarray double[2] doublearry
0_____8 0_______8 0_______8
0 |__A__| 0 | A | | A |
1 |__B__| |___B___| | B |
2 |__C__| 1 | C | | C |
3 |__D__| |___D___| |___D___|
4 |__E__| 2 | E | | E |
5 |__F__| |___F___| | F |
6 |__G__| 3 | G | | G |
7 |__H__| |___H___| |___H___|
8 |__I__| 4 | I | | I |
9 |__J__| |___J___| | J |
10|__K__| 5 | K | | K |
11|__L__| |___L___| |___L___|
12|__M__| 6 | M | | M |
13|__N__| |___N___| | N |
14|__O__| 7 | O | | O |
15|__P__| |___P___| |___P___|

all pointing at the same array in memory.
I can use a java.nio.ByteBuffer to put a double into a ByteArray, and
view it as both 8 bytes and 2 integers etc.

but I cannot create an array of one type and then place it straight into
a DatagramPacket constructor as the buffer to use for the data like
this:

sendDouble( double[] data , offset , length , address , port){

//copy double[] data into new byte[] newData

dsocket.send( new DatagramPacket( newData , offset , length,
address , port));
}

without doing a memory copy of some kind to get the data out of the
double array into a byte array even though they contain the same
bytes...
hope that makes sense.

Is java designed to prevent this kind of memory tampering, even if the
view would be read only?

I'll just have to settle for a single memory copy( + the one from the
DatagramPacket to the network interface w3hich I can't do anything
about) I suppose.

Thanks
Neomorph wrote:

On Wed, 06 Aug 2003 11:36:59 +0100, matt melton <ha***********@yahoo.co.uk>
two-finger typed:
Hi there,
I am trying to write a method that accepts an array of any primitive
type and will return the same array without copying memory as an array
of bytes.


You will have to overload the method (i.e. write a different method for
each type of array).

ie. I'd like to be able to do something like:

char[] chars = "Hello!";


You are assigning a String object to an array ?
Use:
String hello = "Hello!" ;
char [] chars = new char [hello.length()];
hello.getChars(0,hello.length(),chars,0);

Or:
char [] chars = getChars("Hello!");

with an extra (local) method:
public char [] getChars(String s) {
char [] chars = new char [s.length()];
hello.getChars(0,s.length(),chars,0);
}
byte[] bytes = (byte[]) chars;


bytes and chars are two different things.
chars are 16 bit (Unicode), bytes are 8 bit.

The resulting bytes out of a string would have to represent an encoding of
the string to support Unicode characters above the codevalue 255.

Have a look at the String API for more information.

Converting doubles and floats into bytes may pose even more of a challenge,
if you want the storage to be according to IEEE standards.
Have you looked at Serialization ?

which obviously won't work.

Is there any way of casting arrays?

Thanks for any help you can give me.

Matt


Cheers.

Jul 17 '05 #4

P: n/a
Hi there,
it's just a question of effeciency, I don't like to copy memory when I
don't have to, but If I must I must. If it makes the program more
stable secure etc..
then I'll have to like it.
It just seemed a bit of waste to have the same information stored in
two places with identical data just so it can be accessed in a
particular way. I guess it's due to the protability reasons etc...

Cheers

Actually, you don't need to 'cast' arrays of primitives to arrays of bytes.

What you need is something else (I don't know what, exactly), but you
think, for some reason, that your goal can be acomplished by doing
something like that (treating a block of memory like an array of bytes,
regardless of what's inside that block. That's precisely what you shouldn't
even think about, in Java. Go back to C for stunts like that. (-: )

So, it would be better if you went one step back and said what you really
wanted to do, instead of asking detailed help on something that could
easily be a bad idea in the first place.

Jul 17 '05 #5

P: n/a
On 7 Aug 2003 01:25:18 -0700, ha***********@yahoo.co.uk (matt melton)
two-finger typed:
Hi there,
it's just a question of effeciency, I don't like to copy memory when I
don't have to, but If I must I must. If it makes the program more
stable secure etc..
then I'll have to like it.
It just seemed a bit of waste to have the same information stored in
two places with identical data just so it can be accessed in a
particular way. I guess it's due to the protability reasons etc...
Not just for portability, but also for security and bug repellant.

Basically data in memory is always typed in java, to stop the possibility
of bugs with respect to wrong interpretation of memory content (as code for
example - which is an occurence that makes hacking Microsoft Operating
Systems, browsers and even their game console so relatively easy).

An array, for example, is more than just a series of memory locations fora
specific datatype, it also holds the unchangable length of the array.

You could probably use JNI if you are so bent on reusing memory, but that
would make your application non-portable and more open to bugs...

Cheers

Actually, you don't need to 'cast' arrays of primitives to arrays of bytes.

What you need is something else (I don't know what, exactly), but you
think, for some reason, that your goal can be acomplished by doing
something like that (treating a block of memory like an array of bytes,
regardless of what's inside that block. That's precisely what you shouldn't
even think about, in Java. Go back to C for stunts like that. (-: )

So, it would be better if you went one step back and said what you really
wanted to do, instead of asking detailed help on something that could
easily be a bad idea in the first place.


Cheers.
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.