Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting char array into byte array and back again

I'm looking to convert a Java char array to a byte array without creating an intermediate String, as the char array contains a password. I've looked up a couple of methods, but they all seem to fail:

char[] password = "password".toCharArray();  byte[] passwordBytes1 = new byte[password.length*2]; ByteBuffer.wrap(passwordBytes1).asCharBuffer().put(password);  byte[] passwordBytes2 = new byte[password.length*2]; for(int i=0; i<password.length; i++) {     passwordBytes2[2*i] = (byte) ((password[i]&0xFF00)>>8);      passwordBytes2[2*i+1] = (byte) (password[i]&0x00FF);  }  String passwordAsString = new String(password); String passwordBytes1AsString = new String(passwordBytes1); String passwordBytes2AsString = new String(passwordBytes2);  System.out.println(passwordAsString); System.out.println(passwordBytes1AsString); System.out.println(passwordBytes2AsString); assertTrue(passwordAsString.equals(passwordBytes1) || passwordAsString.equals(passwordBytes2)); 

The assertion always fails (and, critically, when the code is used in production, the password is rejected), yet the print statements print out password three times. Why are passwordBytes1AsString and passwordBytes2AsString different from passwordAsString, yet appear identical? Am I missing out a null terminator or something? What can I do to make the conversion and unconversion work?

like image 612
Scott Avatar asked Feb 08 '11 10:02

Scott


People also ask

Can char be converted to byte?

// ascii char to byte. Method 2: Using ToByte() Method: This method is a Convert class method. It is used to converts other base data types to a byte data type.

How do you split Bytearray?

Split Byte Array In Java, we can use ByteBuffer or System. arraycopy to split a single byte array into multiple byte arrays. For example, this 000102030a0b0c0d1a1b1c1d2f2f is a byte array (14 bytes) in hex representation, it is a combined of cipher (8 bytes) + nonce (4 bytes) + extra (2 bytes).

Can you convert a char array to string?

Use the valueOf() method in Java to copy char array to string. You can also use the copyValueOf() method, which represents the character sequence in the array specified. Here, you can specify the part of array to be copied.

How do you initialize Bytearray?

Assigning Elements to byte array in Java In Java, we assign elements to the Java array by indexing only. In this example, you can see, we have declared an array for byte with the size of 5. Then we have assigned values to it with the help of indexing. And printing all values with the help of for-loop iteration.


1 Answers

Conversion between char and byte is character set encoding and decoding.I prefer to make it as clear as possible in code. It doesn't really mean extra code volume:

 Charset latin1Charset = Charset.forName("ISO-8859-1");   charBuffer = latin1Charset.decode(ByteBuffer.wrap(byteArray)); // also decode to String  byteBuffer = latin1Charset.encode(charBuffer);                 // also decode from String 

Aside:

java.nio classes and java.io Reader/Writer classes use ByteBuffer & CharBuffer (which use byte[] and char[] as backing arrays). So often preferable if you use these classes directly. However, you can always do:

 byteArray = ByteBuffer.array();  byteBuffer = ByteBuffer.wrap(byteArray);    byteBuffer.get(byteArray);       charBuffer.put(charArray);  charArray = CharBuffer.array();  charBuffer = ByteBuffer.wrap(charArray);  charBuffer.get(charArray);       charBuffer.put(charArray); 
like image 197
Glen Best Avatar answered Sep 20 '22 21:09

Glen Best