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
• 26,740 points
3,004 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,370 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
12,449 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
• 26,740 points
1,503 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,100 points
896 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
• 26,740 points
1,359 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
1,923 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
• 26,740 points
45,219 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
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,370 points
631 views
+1 vote
3 answers

How to convert timestamp to readable format in blockchain.info?

You are talking about the block_time property ...READ MORE

answered Apr 29, 2019 in Blockchain by Raghu
10,640 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP