Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


JAVA: Convert Byte[] to Char[] and other way.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 NightMarez   Members   -  Reputation: 100

Like
0Likes
Like

Posted 11 December 2005 - 01:53 AM

Hi there. Dunno if this is correct forum to post this on, but is it possible in java to convert a byte[] to char[] and other way around. This is for a server. i got this code
              byte[] decrypt = new byte[length - 2];
                char[] data = new char[length - 2];
        
                System.arraycopy(incoming, 2, data, 0, decrypt.length);
    
                Encryption.prepareKeys(client);
                //Decrypt the data
                for(int i = 0; i < 1; ++i)
                    data = Encryption.decrypt(data);

                System.arraycopy(decrypt, 0, data, 0, decrypt.length);


incoming is also a byte[] buffer it copy incomming to data, decrypt data, copy data to decrypt, then the decryptet data should be in decrypt. it gives me this error tho : Server thread[C] started java.lang.ArrayStoreException at java.lang.System.arraycopy(Native Method) at server.loginserver.ClientThread.run(ClientThread.java:143) Server thread[C] stopped im using jdk 1.5.0 update 6 and Eclipse as dev tool. Thanks.

Sponsor:

#2 Trap   Members   -  Reputation: 684

Like
0Likes
Like

Posted 11 December 2005 - 01:58 AM

char and byte are different things in Java. char can hold all unicode chars which means it has to have >16 bits.

Quote:
arraycopy throws a ArrayStoreException when:
-The src argument and dest argument refer to arrays whose component types are different primitive types.


#3 snk_kid   Members   -  Reputation: 1312

Like
0Likes
Like

Posted 11 December 2005 - 02:07 AM

You want to look into the classes Charset, CharsetDecoder, CharsetEncoder to convert to/from a character buffer and a byte buffer.

#4 NightMarez   Members   -  Reputation: 100

Like
0Likes
Like

Posted 11 December 2005 - 03:11 AM

Maybe an exsample on how to do it ?

Im still a bit lost :o

#5 NightMarez   Members   -  Reputation: 100

Like
0Likes
Like

Posted 11 December 2005 - 03:19 AM

tryed this:


byte[] decrypt = new byte[length - 2];
char[] data = new char[length - 2];
System.arraycopy(incoming, 2, decrypt, 0, decrypt.length);

_char.newDecoder().decode(decrypt, data, true);




however it says :

The method decode(ByteBuffer, CharBuffer, boolean) in the type CharsetDecoder is not applicable
for the arguments (byte[], char[], boolean)

#6 NightMarez   Members   -  Reputation: 100

Like
0Likes
Like

Posted 11 December 2005 - 04:11 AM

found a sollution, takes a bit more ram, but o well.

#7 snk_kid   Members   -  Reputation: 1312

Like
0Likes
Like

Posted 11 December 2005 - 05:10 AM

Quote:
Original post by NightMarez
The method decode(ByteBuffer, CharBuffer, boolean) in the type CharsetDecoder is not applicable for the arguments (byte[], char[], boolean)


The point is you use the "array" method to get the underlying charater/byte array.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS