Java: convert a byte array to a hex string?

From the discussion here, and especially this answer, this is the function I currently use: private static final char[] HEX_ARRAY = “0123456789ABCDEF”.toCharArray(); public static String bytesToHex(byte[] bytes) { char[] hexChars = new char[bytes.length * 2]; for (int j = 0; j < bytes.length; j++) { int v = bytes[j] & 0xFF; hexChars[j * 2] = … Read more

Converting a byte array into a hex string

As I am on Kotlin 1.3 you may also be interested in the UByte soon (note that it’s an experimental feature. See also Kotlin 1.3M1 and 1.3M2 announcement) E.g.: @ExperimentalUnsignedTypes // just to make it clear that the experimental unsigned types are used fun ByteArray.toHexString() = asUByteArray().joinToString(“”) { it.toString(16).padStart(2, ‘0’) } The formatting option is … Read more

Convert a String of Hex into ASCII in Java

Just use a for loop to go through each couple of characters in the string, convert them to a character and then whack the character on the end of a string builder: String hex = “75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000”; StringBuilder output = new StringBuilder(); for (int i = 0; i < hex.length(); i+=2) { String str = hex.substring(i, … Read more

Sending binary data in javascript over HTTP

By default, jQuery serializes the data (passed in data property) – and it means 0xFD008001 number gets passed to the server as ‘4244668417’ string (10 bytes, not 4), that’s why the server treats it not as expected. It’s necessary to prevent such behaviour by setting $.ajax property processData to false: By default, data passed in … Read more

how to convert negative integer value to hex in python

Python’s integers can grow arbitrarily large. In order to compute the raw two’s-complement the way you want it, you would need to specify the desired bit width. Your example shows -199703103 in 64-bit two’s complement, but it just as well could have been 32-bit or 128-bit, resulting in a different number of 0xf‘s at the … Read more