I've got a String like this:
init_thread = "2b11020000ed"
I have to send this string via bluetooth, for what I do this:
byte[] init = init_thread.getBytes();
GlobalVar.mTransmission.write(init);
What I need is to define that the init_thread
string is an hex string before converting it to bytes, because if I do this way, it is getting it wrong:
What is doing now = 2(1byte), b(1byte), 1(1byte), 1(1byte)...
What must do = 2b(1byte), 11(1byte), 02(1byte)...
Now, let's convert a hexadecimal digit to byte. As we know, a byte contains 8 bits. Therefore, we need two hexadecimal digits to create one byte.
To convert byte array to a hex value, we loop through each byte in the array and use String 's format() . We use %02X to print two places ( 02 ) of Hexadecimal ( X ) value and store it in the string st .
Hex String – A Hex String is a combination of the digits 0-9 and characters A-F, just like how a binary string comprises only 0's and 1's. Eg: “245FC” is a hexadecimal string. Byte Array – A Java Byte Array is an array used to store byte data types only. The default value of each element of the byte array is 0.
Convert Hex String to byte Array in Java. To convert hex string to byte array, you need to first get the length of the given string and include it while creating a new byte array. byte[] val = new byte[str.length() / 2]; Now, take a for loop until the length of the byte array.
You can easily convert string to byte[] in one line: var byteArray = Encoding.ASCII.GetBytes(string_with_your_data); – mik-t Jun 6 '13 at 22:02. @mik-T, a hex string is in some format like 219098C10D7 which every two character converts to one single byte. your method is not usable. – AaA Dec 12 '14 at 7:52.
String to bytes converter tool What is a string to bytes converter? This browser-based program converts a string to a byte array. The string is split into individual characters and then for each character, the program finds its byte representation, and prints it in the output area in the hexadecimal base.
Hex String – A Hex String is a combination of the digits 0-9 and characters A-F, just like how a binary string comprises only 0’s and 1’s. Eg: “245FC” is a hexadecimal string.
Convert hex to byte and byte to hex.
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len/2];
for(int i = 0; i < len; i+=2){
data[i/2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16));
}
return data;
}
final protected static char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
public static String byteArrayToHexString(byte[] bytes) {
char[] hexChars = new char[bytes.length*2];
int v;
for(int j=0; j < bytes.length; j++) {
v = bytes[j] & 0xFF;
hexChars[j*2] = hexArray[v>>>4];
hexChars[j*2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
If we want to convert hex to byte array, we should make sure that hex string length should be of even length. Below method handles this
public static byte[] hexToByteArray(String hex) {
hex = hex.length()%2 != 0?"0"+hex:hex;
byte[] b = new byte[hex.length() / 2];
for (int i = 0; i < b.length; i++) {
int index = i * 2;
int v = Integer.parseInt(hex.substring(index, index + 2), 16);
b[i] = (byte) v;
}
return b;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With