5

I have a String that I have to concatenate with an byte array, so I tried this

String msg = "msg to show";

byte[] msgByte = new byte[msg.length()];

try {
msgByte = msg.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

byte[] command = {2,5,1,5}

byte[] c = new byte[msgByte.length + command.length];
System.arraycopy(command, 0, c, 0, command.length);
System.arraycopy(msjByte, 0, c, command.length, msjByte.length);

for(Byte bt:c)
    System.out.println(bt+"");

This is the output:
2 5 1 5 109 115 103 32 ...

but the result that I'm looking for is this
2 5 1 5 m s g ...

I need it in one array cause it's used as a command for a bluetooth printer.

Is there a way, any suggestions?

Thanks in advance! :)

2
  • You have the right result. ascii(109) = m. Is this a fairly low level interface, mixing command codes with strings? Commented Aug 6, 2011 at 2:30
  • My mistake, I thought that I was missing something with the ASCII codes in the array because the example command for the printer come something like this: byte[] ESC_Z2 = {0x1b, 0x5a, 0x00 , 0x51, 0x05, 0x14 , 0x00 ,'m', 'e', 's', 's','a','g','e' }; So I tried sending the pure ASCII code and it didn't work, but it was due a parameter on the command Commented Aug 6, 2011 at 3:13

2 Answers 2

4

You can't have a byte array containing '2 5 1 5 m s g'. From the documentation:

The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).

I can't envisage a scenario where you would actually want to join un-encoded bytes with a string, but here's a solution that returns a char[].

public static void main(String[] args) {
    final String msg = "msg to show";
    final byte[] command = { 2, 5, 1, 5 };

    // Prints [2, 5, 1, 5, m, s, g,  , t, o,  , s, h, o, w]
    System.out.println(Arrays.toString(concat(msg, command)));
}

private static char[] concat(final byte[] bytes, final String str) {
    final StringBuilder sb = new StringBuilder();
    for (byte b : bytes) {
        sb.append(b);
    }
    sb.append(str);
    return sb.toString().toCharArray();
}
Sign up to request clarification or add additional context in comments.

Comments

1

An alternative would be to do this...

String msg = "msg to show";
char[] letters = msg.toCharArray();
byte[] command = {2,5,1,5};
String result;
for (String str: command) {
    result += str + " ";
}
for (String str: letters) {
    result += str + " ";
}
System.out.println(result);

4 Comments

you can have characters in bytes, you just need to know what you are doing. There is nothing wrong in presenting chars < 127 as bytes as long as you are clear on the encoding.
Thank you for catching that! I'll change my answer.
@Jochen: I disagree. The OP clearly states that the result he's looking for is '2 5 1 5 m s g'. A byte array can only contain integers. I know what you mean but the OP was not satisfied with 'encoding' the characters as he already tried that (2 5 1 5 109 115 103 32).
which, if 2 is the value of command byte he wants to send, is just a matter of interpretation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.