I'm trying to make an application that will send a sysex message to a Roland device. I found an example and tried to modify it a bit for my own needs:
protected void transmitSYSEX(String byteString)
{
SysexMessage sysx = new SysexMessage();
int lengthInBytes = byteString.length() / 2;
byte sysxMsg[] = new byte[lengthInBytes];
String message = "";
for (int i = 0; i < lengthInBytes; i++)
{
sysxMsg[i] = (byte) Integer.parseInt(
byteString.substring(i * 2, i * 2 + 1), 16);
}
for (int i = 0 ; i < sysxMsg.length ; i++)
message += sysxMsg[i];
textView.setText(message);
try
{
sysx.setMessage(sysxMsg, sysxMsg.length);
}
catch (InvalidMidiDataException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
out_queue.add(sysx);
}
The byteString is a string that contains the following hex message: F0411000004F1200020201070272F7 However, my control printout at the line textView.setText(message) returns the following string: 15410041000000715
Not quite sure where it goes wrong, shouldn't the textView message be the same as the input message?
Regards /M
I solved the problem by using this algorithm:
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));
Provided in this discussion: Convert a string representation of a hex dump to a byte array using Java?
Not 100% certain about why my returned array is all jumbled, I guess the substring isn't quite up to the job for some reason.
Cheers /M