0

I'm trying to preserve the password as char[] but in the second step, I'm converting it to a String again. Is there a way to to this in a better way?

char[] password = {'g', 'r', 'e'};
String toEncode = username + new String(password);
byte[] encoding = Base64.encodeBase64(toEncode.getBytes(Charset.forName(StandardCharsets.UTF_8.name())));

One option would be:

char[] toEncode = username.toCharArray() + password;
byte[] encoding = Base64.encodeBase64(new String(toEncode).getBytes(Charset.forName(StandardCharsets.UTF_8.name())));

What I'm asking is is there a better way to achieve it without using new String()

7
  • @Michael it's more: how to turn a char array to byte array without an intermediate step turning it to String Commented Jul 10, 2018 at 8:40
  • @jhamon that's what I'm suggesting, I'm asking is there a better way to do it? Commented Jul 10, 2018 at 8:40
  • What I'm asking is is there a better way to achieve it without using new String() Commented Jul 10, 2018 at 8:42
  • The 2nd answer is what you want: stackoverflow.com/questions/5513144/converting-char-to-byte/… Commented Jul 10, 2018 at 8:44
  • @Michael I know, I reported the dupe. But it looks like OP didn't read the correct answer Commented Jul 10, 2018 at 8:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.