Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Types Conversion

Thread view: 
andrewzzz - 21 Aug 2006 16:28 GMT
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!!!!!
Oliver Wong - 21 Aug 2006 16:45 GMT
> Hi Guys,
> I have some problems about converting types..
[quoted text clipped - 3 lines]
> 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
Lasse Reichstein Nielsen - 21 Aug 2006 17:00 GMT
> 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
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

M.J. Dance - 22 Aug 2006 10:27 GMT
> Hi Guys,
> I have some problems about converting types..
[quoted text clipped - 6 lines]
>
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.