Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Byte array to Hex string conversion in javascript

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

like image 636
Actung Avatar asked Dec 16 '15 10:12

Actung


People also ask

How do you convert a byte array to a hexadecimal string?

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);

How do you convert bytes to hexadecimal?

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 .

Is hex same as byte?

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.

How do I convert a byte array to a hex value?

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.

How to convert an array of bytes to a string with JavaScript?

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.

How do I get hexadecimal string in Java?

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.

What is hexadecimal representation of BigInteger in Java?

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.


1 Answers

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

like image 123
Bergi Avatar answered Sep 29 '22 10:09

Bergi