Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
36
rated 0 times [  42] [ 6]  / answers: 1 / hits: 17797  / 12 Years ago, thu, december 13, 2012, 12:00:00

I have a string that contains hexadecimal string (content of a utf8 string )



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


I need to convert it back to a javascript string. how to do it ?


More From » javascript

 Answers
205
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, '%$&'));


This solution actually handles UTF-8.



The idea is to put a % in front of every pair of hex digits (thus creating a URL encoded string), then letting decodeURIComponent handle the details (in particular, it will correctly decode multi-byte UTF-8 characters).


[#81436] Wednesday, December 12, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
isaacvalentinn

Total Points: 325
Total Questions: 120
Total Answers: 131

Location: North Korea
Member since Tue, Jun 16, 2020
4 Years ago
;