-2

I'm new to Java and I'm sure this is very simple. I've been trying to convert a string to byte[] can't seem to do it. I'm using it for a printer.

 ArrayList<byte[]> commands = new ArrayList<byte[]>();
 commands.add(new byte[] { 0x1b, 0x2d, 0x00 });

The above works fine but what i need is to be able to take the 0x1b, 0x2d, 0x00 value and insert it from a string like

 String hexcode = "0x1b, 0x2d, 0x00";
 commands.add(new byte[] { hexcode });

I've tried a ton of stuff can't seem to get it to go.

frogatto
  • 28,539
  • 11
  • 83
  • 129
user1054513
  • 605
  • 3
  • 13
  • 24

2 Answers2

2

Try this:

String str = "\u001b\u002d\u0000";
byte[] bytes = str.getBytes(StandardCharsets.ISO_8859_1);

This produces a byte array equivalent to new byte[] {0x1b, 0x2d, 0x00}.

Explanation:

  • A Java String is a sequence of characters
  • The \uxxxx is how you write a Unicode literal in Java code.
  • The getBytes(Charset) method does a characters to bytes encoding using the Charset provided.
  • The Unicode characters \u0000 through \u00ff map straight to 0x00 through 0xff in the ISO_8859_1 (or Latin-1) character set.
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I just tried it this way and it printed out \u001b\u002d\u0000 as text – user1054513 May 14 '16 at 06:22
  • Did you use a string literal? That's what my code requires! If you are not asking about an input string provided as a string literal, then your question should not use string literals as examples. It is >>misleading<<. – Stephen C May 14 '16 at 06:23
  • yes, i believe the 0x1d, 0x21, 0x11 is suppose to be a array thats why there are {} around it then convert the array to bytes i think – user1054513 May 14 '16 at 06:25
  • Yes. But if the problem is how to populate an arbitrary (binary) byte array **from a String literal**, then my solution is the simpler way to do it. If you want to populate it from something else (e.g. some particular String format) then please make that clear in the question. Also, please make the specification of the String format unambiguous. – Stephen C May 14 '16 at 06:34
0

Try this. Very simple approach:

import java.util.ArrayList;

public class HelloWorld {

    public static void main(String[] args) {
        String hexcode = "0x1b, 0x2d, 0x00";
        String[] stringArray = hexcode.split(",");

        ArrayList<byte[]> byteArray = new ArrayList<byte[]>();

        for (int x = 0; x < stringArray.length; x++) {
            byteArray.add(stringArray[x].getBytes());
        }
        System.out.println(byteArray);
    }
}

We just split the first string and create a string array. Then we loop through it generating the bytes for each element pushing those into the ArrayList (which you had in your example).

Spits out:

[B@74a14482, [B@1540e19d, [B@677327b6]
Adam Gerard
  • 708
  • 2
  • 8
  • 23
  • I was trying that but what i need is those hexcodes to end up being excactly how they look as a byte not converted and it gives me a incompatable type – user1054513 May 14 '16 at 08:55
  • It's because the ArrayList is actually a two-dimensional collection. What we have is an ArrayList of byte arrays. Grab the first element of each array and it'll be the byte. Use the correct charset to make it look the way you want. – Adam Gerard May 14 '16 at 16:53