Types Conversion

A

andrewzzz

Hi Guys,
I have some problems about converting types..

1-What is the fastest way to convert from Calendar to byte array?
2-What is the fastst way to convert from String to byte array?
3-How do I convert Certificate to byte array?
4-How do I convert PrivateKey to byte array?
5-How do I merge multiple byte arrays into a bigger one?

thanks a lot for your help!!!!!
 
O

Oliver Wong

andrewzzz said:
Hi Guys,
I have some problems about converting types..

1-What is the fastest way to convert from Calendar to byte array?
2-What is the fastst way to convert from String to byte array?
3-How do I convert Certificate to byte array?
4-How do I convert PrivateKey to byte array?

There are multiple ways of converting from one format to another, just
like there are multiple ways of converting letters to numbers. For example,
you might map A to 1, B to 2, C to 3, etc. Or you might map A to 0, B to 1,
C to 2, etc. Before worrying about speed, you should probably think about
what values map onto what other values first.
5-How do I merge multiple byte arrays into a bigger one?

There are multiple ways of doing this. The simplest would be to
concatenate the sequences of bytes together, but then you lose the
information of where each sequence starts and end. To get around this, you
might want to additionally encode the length of each sequence, for example,
or have some sort of special "end of sequence" marker.

- Oliver
 
L

Lasse Reichstein Nielsen

andrewzzz said:
I have some problems about converting types..

Why do you need to convert them?
1-What is the fastest way to convert from Calendar to byte array?

What should that byte array satisfy?
Obviosuly, you could serialize the Calendar into a byte array, but
that's hardly fast. On the other hand, it is probably the simplest
to implement when you want to serialize several different objects
into one byte sequence.

Or you could do
long time = calendar.getTimeInMillis();
byte[] timeBytes = new byte[]{
(byte) (time >> 56),
(byte) ((time >> 48) & 255),
(byte) ((time >> 40) & 255),
(byte) ((time >> 32) & 255),
(byte) ((time >> 24) & 255),
(byte) ((time >> 16) & 255),
(byte) ((time >> 8) & 255),
(byte) (time & 255),
};
or something similar.
2-What is the fastst way to convert from String to byte array?

Perhaps
string.getBytes() // or better yet, pick an encoding

Again, it depends on what you need to use it for.
3-How do I convert Certificate to byte array?

java.security.cert.Certificate, javax.security.cert.Certificate,
or another specialization of java.security.Certificate?

Probably either getEncoded() or encode(ByteStream).
4-How do I convert PrivateKey to byte array?

The PrivateKey interface also has a getEncoded() method.
5-How do I merge multiple byte arrays into a bigger one?

byte[] bigArray = new byte[array1.length + array2.length];
System.arraycopy(array1, 0, bigArray, 0, array1.length);
System.arraycopy(array2, 0, bigArray, array1.length, array2.length);

Obviously this is not storing enough information to split the big
array into smaller ones.

I think what is worrying me is your request for a "fastest" way,
since that's probably less understandable and maintainable than
just the simplest way.

/L
 
M

M.J. Dance

andrewzzz said:
Hi Guys,
I have some problems about converting types..

1-What is the fastest way to convert from Calendar to byte array?
2-What is the fastst way to convert from String to byte array?
3-How do I convert Certificate to byte array?
4-How do I convert PrivateKey to byte array?
5-How do I merge multiple byte arrays into a bigger one?

thanks a lot for your help!!!!!

1- The question is whether you want to deserialize it, too. Moreover, do you
want the complete information restored or just a timestamp.

2- Fastest? String.getBytes() is a pretty good candidate. Perhaps you could do
better if you have an additional information regarding your strings' structure.

3- and 4- sound suspiciously like you want to store them somewhere. There are
better ways to do that than simply "serializing" a object.

As for 5-, you have to allocate an array as big as to accommodate both arrays
and then use System.arraycopy twice. An alternative is to use ByteBuffer, two
..put()s and an .array(), which basically does the same thing behind the scenes.
 

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

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top