Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reverse order, byte wise, of a string in JavaScript?

I have ded3e8c2e3460a97500c09d752a83c4eb44eda90998e33ce8d346a1174c0b97f and I want 7fb9c0....

I am using lodash and where's what I have so far

mytxid = 'ded3e8c2e3460a97500c09d752a83c4eb44eda90998e33ce8d346a1174c0b97f'
reverseTxid = _.chunk mytxid.split(''), 2
reverseTxid = reverseTxid.reverse()
reverseTxid _.flattenDeep reverseTxid

However, I get an error on the .reverse(): [TypeError: object is not a function]

What am I doing wrong and what's a better way to do it?

like image 336
Shamoon Avatar asked Oct 18 '25 18:10

Shamoon


1 Answers

You can split the String into bytes by matching every two hex digits, reverse the returned array, then join the array back into a String:

var s = "ded3e8c2e3460a97500c09d752a83c4eb44eda90998e33ce8d346a1174c0b97f";
s.match(/[a-fA-F0-9]{2}/g).reverse().join('')
// "7fb9c074116a348dce338e9990da4eb44e3ca852d7090c50970a46e3c2e8d3de"
like image 139
Hunter McMillen Avatar answered Oct 20 '25 09:10

Hunter McMillen