title: Send hex command to ESC/POS Printer Android
body: <p>I am working on an application that needs to print directly to an ESC/POS printer.  Printing is fine, however when issuing commands to the printer they just print!!
  I'm trying to convert some c# code and the commands I'm trying to send are hex strings as follows:</p>
  
  <pre><code>public static String PRINTLOGOCOMPANY = "\x1c\x70\x01\x30";
  </code></pre>
  
  <p>Of course I understand that there are illegal escape characters in this so I changed it to:</p>
  
  <pre><code>"\\x1c\\x70\\x01\\x30"
  </code></pre>
  
  <p>I then converted to a byte array and tried sending it via a dataoutput stream like so:</p>
  
  <pre><code>String WIDTH_1 ="\\x1d\\x57\\x120\\x01";
  
  Log.i("Width String: ", WIDTH_1);
  final byte [] width = WIDTH_1.getBytes();
  final int portNo =  xxxx;
  final String ipAddress = "xxx.xxx.x.xxx";
  
  Thread thread = new Thread() {
      @Override
      public void run() {
          try {
              Socket sock = new Socket(ipAddress, portNo);
              DataOutputStream dOut = new DataOutputStream(sock.getOutputStream());
              dOut.writeInt(width.length);
              dOut.write(width);
              dOut.close();
              sock.close();
          } catch (UnknownHostException e) {
  
              e.printStackTrace();
              Log.i("Unknown Host Exception Error: ", String.valueOf(e));
          } catch (IOException e) {
              e.printStackTrace();
              Log.i("IO Exception Error: ", String.valueOf(e));
          }
      }
  };
  thread.start(
  </code></pre>
  
  <p>but it just prints it on the other end;</p>
  
  <p>Could anyone offer any advice on this Id greatly appreciate it.</p>