---
title: "Char Array to Byte array conversion and convert back again to char Arrray"  
description: "Char Array to Byte array conversion and convert back again to char Arrray"  
author: "Tom Cruser"  
published: 2016-01-14  
updated: 2023-06-27  
canonical: https://www.mindstick.com/forum/33877/char-array-to-byte-array-conversion-and-convert-back-again-to-char-arrray  
category: "android"  
tags: ["android", "array"]  
reading_time: 2 minutes  

---

# Char Array to Byte array conversion and convert back again to char Arrray

I want to [convert](https://www.mindstick.com/forum/2093/configurationmanager-appsettings-convert-n-to-n-why) a Java char array to a byte array without creating an intermediate [String](https://www.mindstick.com/articles/1527/string-split-in-c-sharp),since the char [array contains](https://www.mindstick.com/forum/23089/in-java-how-can-i-test-if-an-array-contains-a-certain-value) a [password](https://www.mindstick.com/blog/118/retriving-user-password-by-using-stored-procedure-in-asp-dot-net). I've used a couple of [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best), but they all found useless :\

```
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](https://yourviews.mindstick.com/story/1489/the-countries-that-dominate-world-fossil-fuel-production), the password is rejected), yet the [print](https://www.mindstick.com/blog/301752/types-of-3d-printing-technology) statements print out password three times. Why are passwordBytes1AsString and passwordBytes2AsString different from passwordAsString, yet appear identical? Am I [missing](https://answers.mindstick.com/qa/52138/international-missing-children-s-day-2019-was-observed-on) out a null terminator or something? What can I do to make the [conversion](https://www.mindstick.com/forum/1734/conversion-from-32-bit-integer-to-4-chars) and unconversion work?

## Replies

### Reply by Aryan Kumar

Sure, here is how to convert a char [array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net) to a byte array and back again to a char array:

Code snippet

```plaintext
import java.nio.charset.StandardCharsets;

public class CharArrayToByteArray {

    public static void main(String[] args) {
        // Create a char array.
        char[] charArray = "Hello, world!".toCharArray();

        // Convert the char array to a byte array.
        byte[] byteArray = StandardCharsets.UTF_8.encode(charArray);

        // Convert the byte array back to a char array.
        char[] newCharArray = StandardCharsets.UTF_8.decode(byteArray).toCharArray();

        // Print the new char array.
        System.out.println(newCharArray);
    }
}
```

In this code, we first create a char array with the string "Hello, world!". Then, we use the `StandardCharsets.UTF_8.encode()` method to convert the char array to a byte array. The `StandardCharsets.UTF_8.encode()` method takes a char array as input and returns a byte array. The byte array contains the UTF-8 encoding of the char array.

Next, we use the `StandardCharsets.UTF_8.decode()` method to convert the byte array back to a char array. The `StandardCharsets.UTF_8.decode()` method takes a byte array as input and returns a char array. The char array contains the characters that were encoded in the byte array.

Finally, we print the new char array.

### Reply by Anonymous User

The problem is your use of the String(byte[]) constructor, which uses the platform default encoding. That's almost never what you should be doing - if you pass in "UTF-16" as the character encoding to work, your tests will probably pass. Currently I suspect that passwordBytes1AsString and passwordBytes2AsString are each 16 characters long, with every other character being U+0000


---

Original Source: https://www.mindstick.com/forum/33877/char-array-to-byte-array-conversion-and-convert-back-again-to-char-arrray

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
