Reputation: 37
I need to know how to convert two strings into a single byte array. For example, I need to take two numeric values using two Edittext
where one is 850011 and another is 1005.
I need to put this into a single byte array so that byte array will automatically become something like below if I do it manually:
byte[] data = new byte[5];
data[0]=(byte)0x85;
data[1]=(byte)0x00;
data[2]=(byte)0x11;
data[3]=(byte)0x10;
data[4]=(byte)0x05;
How to do it foolproof?
Upvotes: 0
Views: 166
Reputation: 189
You can try using something like:
public byte[] convert(String a, String b) {
int radix = 16; //for hexadecimal conversion
//int radix = 10; //for decimal conversion
String str = a + b;
byte[] result = new byte[str.length() / 2];
for (int i = 0; i < result.length; i++) {
int index = i * 2;
int j = Integer.parseInt(str.substring(index, index + 2), radix);
result[i] = (byte) j;
}
return result;
}
Upvotes: 1