7

My byte array is look like

 byte[] x = { (byte) 0xff , (byte) 0x80  };

how can i convert it to char array {char[] y } where:-

y[0]='f';
y[1] ='f' and so on
3
  • 1
    ... and your "and so on" means what? Presumably you don't mean that every element in the array is 'f', which is what it looks like. Commented Mar 4, 2015 at 12:47
  • 3
    Do you want to convert it to { 'f', 'f', '8', '0' }? Commented Mar 4, 2015 at 12:52
  • @Thilo Why did you marked this as duplicate? The Question is about convert to char array. Convert to string is not equal convert to char array in java. Especially when it concerns the passwords. Commented Nov 8, 2020 at 9:35

1 Answer 1

11

This should help you:

byte[] data = { (byte) 0xff , (byte) 0x80  };
String text = new String(data, "UTF-8");
char[] chars = text.toCharArray();
Sign up to request clarification or add additional context in comments.

5 Comments

That doesn't yield { 'f', 'f', '8', '0' }
Well that wasn't specified so i assumed he wants to convert the bytes into their corresponding character values...
True. The question was clarified later.
Thanks Thilo for the links, sharing code worked for me . byte[] x = {(byte) 0x80,(byte) 0x60}; StringBuilder sb = new StringBuilder(x.length * 2); for(byte b: x) sb.append(String.format("%02x", b & 0xff)); String s = sb.toString(); char[] array = s.toCharArray();
This is unsafe for passwords.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.