Possible Duplicate:
Converting char array into byte array and back again
I have a password in char[]
and i want this password in byte[]
without creating any String
. Please suggest the best possible way.
Possible Duplicate:
Converting char array into byte array and back again
I have a password in char[]
and i want this password in byte[]
without creating any String
. Please suggest the best possible way.
If ASCII or ISO-8859-1 encoding is all you need you can copy one char at a time.
char[] chars = ...
byte[] bytes = new byte[chars.length];
for(int i = 0; i < chars.length; i++)
bytes[i] = (byte) chars[i];
I don't know about any build in tool to do that. You may want to create a utility class that solve the issue for you.
public static byte[] charToByte(char[] array) {
byte[] result = new byte[array.length];
for(int i = 0; i < array.length; i++) {
result[i] = (byte) array[i];
}
return result;
}
How ever this solution is limited by encoding you are using.