I have a byte array of the form [4,-101,122,-41,-30,23,-28,3,..]
which I want to convert in the form 6d69f597b217fa333246c2c8
I'm using below function
function toHexString(bytes) { return bytes.map(function(byte) { return (byte & 0xFF).toString(16) }).join('') }
which is giving me a string of the same form but I suspect that it's not an efficient conversion because the hex string is bit shorter than expected. I think translating should get "0a10a6dc". Please tell me if I'm wrong or is this a right conversion but maybe I'm not using the right byte array
byte array 4,-127,45,126,58,-104,41,-27,-43,27,-35,100,-50,-77,93,-16,96,105,-101,-63,48,-105,49,-67,110,111,26,84,67,-89,-7,-50,10,-12,56,47,-49,-42,-11,-8,-96,-117,-78,97,-105,9,-62,-44,-97,-73,113,96,23,112,-14,-62,103,-104,90,-14,117,78,31,-116,-7
Corresponding conversion 4812d7e3a9829e5d51bdd64ceb35df060699bc1309731bd6e6f1a5443a7f9ceaf4382fcfd6f5f8a08bb261979c2d49fb771601770f2c267985af2754e1f8cf9
To obtain a string in hexadecimal format from this array, we simply need to call the ToString method on the BitConverter class. As input we need to pass our byte array and, as output, we get the hexadecimal string representing it. string hexString = BitConverter. ToString(byteArray);
To convert byte array to a hex value, we loop through each byte in the array and use String 's format() . We use %02X to print two places ( 02 ) of Hexadecimal ( X ) value and store it in the string st .
Each Hexadecimal character represents 4 bits (0 - 15 decimal) which is called a nibble (a small byte - honest!). A byte (or octet) is 8 bits so is always represented by 2 Hex characters in the range 00 to FF.
In the above program, we have a byte array named bytes. To convert byte array to a hex value, we loop through each byte in the array and use String 's format (). We use %02X to print two places ( 02) of Hexadecimal ( X) value and store it in the string st. This is a relatively slower process for large byte array conversion.
Converting an array of bytes to a string with JavaScript is very easy when we use the fromCharCode function, however, if you string have special characters you might want to look at a different solution.
Using the BigInteger Class We can create an object of type BigInteger by passing a signum and byte array. Now, we can generate the hexadecimal String with the help of static method format defined in String class: The format provided will generate a zero-padded lowercase hexadecimal String.
Using the Hexadecimal Representation of BigInteger class in Java is quite an avoided way of converting byte array to hex string due to its slow speed. Here one can also observe that since we deal with numbers and not arbitrary byte strings, this may omit leading zeros in cases.
You are missing the padding in the hex conversion. You'll want to use
function toHexString(byteArray) { return Array.from(byteArray, function(byte) { return ('0' + (byte & 0xFF).toString(16)).slice(-2); }).join('') }
so that each byte transforms to exactly two hex digits. Your expected output would be 04812d7e3a9829e5d51bdd64ceb35df060699bc1309731bd6e6f1a5443a7f9ce0af4382fcfd6f5f8a08bb2619709c2d49fb771601770f2c267985af2754e1f8cf9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With