title: how to send hexadecimal value in java to RS232 Serial port
body: <p>I need to send some commands to my microcontroller.
  Iam using the RXTX library. I can connect to my microcontroller but i have problems to convert my commands to right bytes.</p>
  
  <p>example:
  i want to send 0x80 hex value.</p>
  
  <p>if i convert 128 integer (0x80) to bytes</p>
  
  <pre><code>   byte a = (byte) 128;
  </code></pre>
  
  <p>i get  <code>-128</code></p>
  
  <p>the range from java bytes is <code>-128 - 127</code></p>
  
  <p>but i need to send 128 as a command. How can i convert it?</p>
  
  <p>also i do have other commands to send.</p>
  
  <pre><code>80
  20 F2 40 F8 3F 
  FA 21 40 
  </code></pre>
  
  <p>one of them is 40 bits. </p>
  
  <p>I write to the outputstream, that can accept <code>byte[]</code> or <code>int</code>. </p>
  
  <pre><code>...
    static OutputStream out;
  out.write(byte[]);
  out.write(int); 
  ..
  </code></pre>
  
  <p>But int is out of range with 40bit. How can i parse a 40bit Integer to a byte?</p>
  
  <p><a href="https://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java">This Question helped me</a></p>