Note that there are some explanatory texts on larger screens.

plurals
  1. POHexadecimal over 7F into String in Java
    text
    copied!<p>I need to append some hexadecimal characters to my string. I'm trying this:</p> <pre><code>content += Character.toString((char) Integer.parseInt(Integer.toHexString(originalSize).toString(), 16)); </code></pre> <p>And it's working, but when <code>originalSize</code> is over 127 (7F in hex) it returns me two hexadecimal values.</p> <p>For example, doing this:</p> <pre><code>content += Character.toString((char) Integer.parseInt(Integer.toHexString(176).toString(), 16)); </code></pre> <p>The result is: <code>(content hex numbers) C0 B0</code></p> <p>B0 is 176 in hexadecimal, but I don't know how to remove C0. Any suggestions please? Thanks!</p> <p><strong>EDIT</strong>:</p> <p>I want to send an string to a device via Bluetooth Low Energy. I have an string like this:</p> <p>"ABCABC". In hexadecimal is <code>41 42 43 41 42 43</code>. </p> <p>Now, I want to add the format of this string (because the device is waiting for it), so I add it at the end: <code>41 42 43 41 42 43 7E 06 02</code>, where:</p> <ul> <li><code>7E</code>: beggining of the format</li> <li><code>06:</code> number of chars</li> <li><code>02</code>: specifical format given by manufacturer.</li> </ul> <p>I have the main string and I'm adding this three hexadecimal characters by hand.</p> <p><strong>SOLUTION:</strong></p> <p>Based on Devon_C_Miller answer I found my own solution:</p> <pre><code>contentFormated = new byte[originalSize+3]; for(int i=0;i&lt;originalSize;i++){ contentFormated[i] = content.getBytes()[i]; } contentFormated[originalSize] = 0x7E; contentFormated[originalSize+1] = (byte) 0xB0; contentFormated[originalSize+2] = 0x02; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload