简体   繁体   English

如何在JavaScript中按字节顺序颠倒字符串的顺序?

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

I have ded3e8c2e3460a97500c09d752a83c4eb44eda90998e33ce8d346a1174c0b97f and I want 7fb9c0 .... 我有ded3e8c2e3460a97500c09d752a83c4eb44eda90998e33ce8d346a1174c0b97f我想要7fb9c0 ....

I am using lodash and where's what I have so far 我正在使用lodash ,到目前为止我在哪里

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] 但是,我在.reverse()上收到错误: [TypeError: object is not a function]

What am I doing wrong and what's a better way to do it? 我做错了什么,有什么更好的方法呢?

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: 您可以通过匹配每两个十六进制数字将字符串拆分为字节,反转返回的数组,然后将数组连接回String:

var s = "ded3e8c2e3460a97500c09d752a83c4eb44eda90998e33ce8d346a1174c0b97f";
s.match(/[a-fA-F0-9]{2}/g).reverse().join('')
// "7fb9c074116a348dce338e9990da4eb44e3ca852d7090c50970a46e3c2e8d3de"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM