我有一个包含十六进制字符串的字符串(utf8字符串的内容)
"666f6f6c 6973686e 6573732c 20697420 77617320 74686520 65706f63 68206f66 2062656c 6965662c 20697420 77617320 74686520 65706f63 68206f66 20696e63 72656475 6c697479 2c206974 20776173 20746865 20736561 736f6e20 6f66204c 69676874 2c206974 20776173 20746865 2073656"
Run Code Online (Sandbox Code Playgroud)
我需要将其转换回javascript字符串.怎么做 ?
mel*_*ene 10
var s = "666f6f6c 6973686e 6573732c 20697420 77617320 74686520 65706f63 68206f66 2062656c 6965662c 20697420 77617320 74686520 65706f63 68206f66 20696e63 72656475 6c697479 2c206974 20776173 20746865 20736561 736f6e20 6f66204c 69676874 2c206974 20776173 20746865 2073656";
var r = decodeURIComponent(s.replace(/\s+/g, '').replace(/[0-9a-f]{2}/g, '%$&'));
Run Code Online (Sandbox Code Playgroud)
该解决方案实际上处理UTF-8.
想法是%在每对十六进制数字前面放置一个(从而创建一个URL编码的字符串),然后让decodeURIComponent处理细节(特别是,它将正确解码多字节UTF-8字符).