title: Java: Float to hex and reverse
body: <p>Heyho,</p>
  
  <p>i got a little question. So I got a measurement device and i retrieve the data via modbus. I usually check the values via "Simply Modbus TCP Client" which displays me the correct data. Now I want to automate this process which takes me into trouble. </p>
  
  <p>I get the correct response but the tricky part seems to be the conversion. I get the same response in hex but when I convert it, I won't get the same result. </p>
  
  <p>Take a look at the Screenshot <a href="https://i.stack.imgur.com/KHiFr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KHiFr.png" alt="Simply Modbus Client - Screenshot"></a></p>
  
  <p>There you can see the response on the left, the bytes (4175 AF8A) on the right and the corresponding value (22739113). I expect "bytes" to be a hex representation but I can't convert them into a decimal number. </p>
  
  <p>I wrote a small java-program to display these numbers and their conversions. </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>public static void main(String[] args) {
  
      HashMap&lt;String, Integer&gt; values = new HashMap&lt;&gt;();
      values.put("4175AF52", 22738214);
      values.put("41D61FAC", 1484698204);
      values.put("419A08A0", 109193237);
  
      Iterator it = values.entrySet().iterator();
      while (it.hasNext()) {
          System.out.println("/************************************************/");
          Map.Entry pair = (Map.Entry) it.next();
          int hexIntepretation = (int) Long.parseLong((String) pair.getKey(), 16);
  
          // Print the result
          System.out.println(pair.getKey() + " = " + pair.getValue());
          System.out.println("Expected:\t" + pair.getValue());
          System.out.println("Hex to Int:\t" + hexIntepretation);
          System.out.println("Int to Hex:\t" + Integer.toHexString((int) pair.getValue()));
          System.out.println("Float to Hex:\t" + Float.toHexString((int) pair.getValue()));
          System.out.println("Hex to Int:\t" + Integer.parseInt((String) pair.getKey(), 16));
  
          it.remove(); // avoids a ConcurrentModificationException
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Running this snippet gives this output:</p>
  
  <pre><code>/*******************************************/
  419A08A0 = 109193237
  Expected:       109193237
  Hex to Int:     1100613792
  Int to Hex:     6822815
  Float to Hex:   0x1.a08a06p26
  Hex to Int:     1100613792
  /*******************************************/
  41D61FAC = 1484698204
  Expected:       1484698204
  Hex to Int:     1104551852
  Int to Hex:     587eb25c
  Float to Hex:   0x1.61facap30
  Hex to Int:     1104551852
  /*******************************************/
  4175AF52 = 22738214
  Expected:       22738214
  Hex to Int:     1098231634
  Int to Hex:     15af526
  Float to Hex:   0x1.5af526p24
  Hex to Int:     1098231634
  </code></pre>
  
  <p>No matter what I do, I can't get the values. BUT when I do "float-to-hex", I will find some of the initial values in there. </p>
  
  <p>Example:</p>
  
  <p>417<strong>5AF52</strong> = 22738214</p>
  
  <p>Float to Hex:   0x1.<strong>5af52</strong>6p24</p>
  
  <p>Yet I can't put the pieces together to solve this puzzle. Any help is appreciated!</p>