How to convert byte array to hex string?

0 votes

I have a byte array and want convert it into hex string. I am using the following code to do this:

function toHexString(bytes) {

  return bytes.map(function(byte) {

    return (byte & 0xFF).toString(16)

  }).join('')

}

But I am not getting the right result. Where am I going wrong? How to do this right?

Aug 17, 2018 in Blockchain by digger
• 27,640 points
48 views

1 answer to this question.

0 votes

You are missing the padding in the hex conversion in your code. Try the following code.

function toHexString(byteArray) {

  return Array.from(byteArray, function(byte) {

    return ('0' + (byte & 0xFF).toString(16)).slice(-2);

  }).join('')

}

The output should be:

04812d7e3a9829e5d51bdd64ceb35df060699bc1309731bd6e6f1a5443a7f9ce0af4382fcfd6f5f8a08bb2619709c2d49fb771601770f2c267985af2754e1f8cf9
answered Aug 17, 2018 by slayer
• 29,050 points

Related Questions In Blockchain

+1 vote
2 answers

How to convert INT to STRING in Solidity?

Look at the following code : function uintToString(uint ...READ MORE

answered Jun 27, 2018 in Blockchain by Christine
• 15,790 points
960 views
0 votes
1 answer

how can i convert stub.GetTxTimestamp() to type string?

You can not directly convert the timestamp ...READ MORE

answered Jul 5, 2018 in Blockchain by digger
• 27,640 points
112 views
0 votes
1 answer

How do I convert to Crystal ruby's multiple assignments of Array

When decomposing an Array into a multiple ...READ MORE

answered Aug 10, 2018 in Blockchain by Perry
• 17,020 points
29 views
0 votes
1 answer

Cant Decode Bitcoin Base58 address to byte array

In your for-loop: for (int k = b256.Length ...READ MORE

answered Aug 21, 2018 in Blockchain by digger
• 27,640 points
91 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
140 views
+1 vote
3 answers

Removing double quotes from a string from JSON response in PHP

Just remove the json_encode call, and it should work: $resp ...READ MORE

answered Sep 12, 2018 in Blockchain by digger
• 27,640 points
3,010 views
0 votes
1 answer
0 votes
1 answer

How to convert Bitcoin rate to USD?

You can use blockchain.info API: URL: https://blockchain.info/ticker Response: { "USD" ...READ MORE

answered Aug 21, 2018 in Blockchain by slayer
• 29,050 points
25 views
+1 vote
3 answers