[Asking] Convert String to Byte Array
Hi everyone,
I get confused when to convert string to byte array. let's go to the code
String strKey = "BDE540BD7E91EDACA3B0125FE004F52C";
byte[] bytKey = strKey.getBytes();
System.out.println("len "bytKey.length);
When I run the code, the length of bytKey is 32 not 16. I want the length of bytKey is 16.
So in array should be like this ;
bytKey[0] = 'BD';
bytKey[1] = 'E5';
......
......
bytKey[15] = '2C';
Please share me if anyone know how to solve this,
thanks
Re: [Asking] Convert String to Byte Array
I'm not sure I understand - the String is 32 characters long. Not sure how you expect the bytes of the information of the String any less than 32. That being said, read the API for the getBytes methods of String, there are ways to specify a Charset that differs from your default OS Charset.
Re: [Asking] Convert String to Byte Array
Hi, copeg..
I expect the byte array from the String , may be I'm wrong about array example writing.
The arrray should be like this :
bytKey[0] = 0xBD;
bytKey[1] = 0xE5;
........
........
bytKey[15] = 0x2C;
Btw, I already use utf8, but the length still 32.
Please help me
Re: [Asking] Convert String to Byte Array
You have a string of length 32. Each character of that string is 1 byte (8 bits) per character - hence an array of 32 bytes (based upon the encoding you are using - the default for your system and most likely some type of ASCII character code). I do not know of an encoding which packs the characters as 2 characters/byte. What is wrong with the current encoding?
Re: [Asking] Convert String to Byte Array
Declare an array of bytes. The length of the byte array is equal to the String length()/2.
Code java:
byte [] byteArray = new byte[strKey.length()/2];
Make a loop that goes through the string two chars at a time:
Code java:
for (int i = 0; i < strKey.length(); i+= 2)
Inside the loop
Extract two chars at position i and position i+1 in the String
Set char1 = the character of strKey at index value i
Set char2 = the character of strKey at index value i+1
Convert each char to an integer with the value of that character, radix 16
int1 = Character.digit(char1,16)
Similarly for int2
Convert the two hex values to an 8-bit byte
intValue = int1*16+int2
set byteArray[i/2] equal to the value of intValue, cast to a byte.
Cheers!
Z
Re: [Asking] Convert String to Byte Array
Hi copeg...
The current encoding have length 32. So in the array should be like this
bytKey[0] = 0x42;
bytKey[1] = 0x44;
bytKey[2] = 0x45;
........
........
bytKey[31] = 0x43;
But actually that isn't what I want. The byte array what I want which represent the hexadecimal as 2 characters/byte.
bytKey[0] = 0xBD;
bytKey[1] = 0xE5;
........
........
bytKey[15] = 0x2C;
I got confused how to use bitwise operator.
Re: [Asking] Convert String to Byte Array
Hi Zaphod....
Thanks for your code, so the full code perhaps like this below
Code java:
public static void convertStr2Byte() {
String strKey = "BDE540BD7E91EDACA3B0125FE004F52C";
byte[] byteArray = new byte[strKey.length()/2];
for(int i = 0 ; i < strKey.length(); i+=2) {
char ch1 = strKey.charAt(i);
char ch2 = strKey.charAt(i+1);
int int1 = Character.digit(ch1, 16);
int int2 = Character.digit(ch2, 16);
int value = int1 * 16 + int2;
byteArray[i/2] = (byte) value;
System.out.println("byteArray["+i+"] "+byteArray[i/2]);
}
}
thanks to copeg to explain more details about byte array :)
Re: [Asking] Convert String to Byte Array
As a matter of conventional programming style, I think it would be better to let the convertStr2Byte() method convert and return the array of bytes and leave printing for the calling function:
Code java:
public class TestStr2Byte {
public static void main(String [] args) {
String strKey = "BDE540BD7E91EDACA3B0125FE004F52C";
byte [] bytes = convertStr2Bytes(strKey);
System.out.printf("The String : %s\n", strKey);
System.out.print("The hex bytes : ");
for (int i = 0; i < bytes.length; i++) {
System.out.printf("%02X ", bytes[i]);
}
System.out.println();
} // End main
// The function should do one thing: Convert.
public static byte [] convertStr2Bytes(String str) {
byte [] byteArray = new byte[str.length()/2];
//
// Put your code to convert. Don't print
//
return byteArray;
}// End convertStr2Bytes
Or some such thing.
Output:
Code :
The String : BDE540BD7E91EDACA3B0125FE004F52C
The hex bytes : BD E5 40 BD 7E 91 ED AC A3 B0 12 5F E0 04 F5 2C
Cheers!
Z
Re: [Asking] Convert String to Byte Array
I may have missed the full question when reading your posts as the word 'hex' was never used until Zaphod's posts rang a bell in my head...so your string is hexadecimal? An alternative to the method above is to loop over each hex value (every 2 chars of the string) and use Integer.parseInt with a radix of 16 to get the byte value