javascript How to compare two strings with different encodings
const buf1 = Buffer.from('c2bf43c3b36d6f20657374c3a1733f', 'hex')
const buf2 = Buffer.from('fffebf004300f3006d006f002000650073007400e10073003f00', 'hex')
console.log(buf1.toString())
console.log(buf2.toString('utf16le'))
console.log(buf1.toString() === buf2.toString('utf16le'))
Output
¿Cómo estás?
¿Cómo estás?
false
The encodings are different but the characters they represent are the same. How to compare to return true
Javascript's
===
determines whether the strings are the same based on the unicode code point valuebuf1.toString() differs from buf2.toString('utf16le') because the latter contains zero-width characters to represent endianness.
If you do not consider such whitespace characters, you can use
to determine whether the characters are the same.localeCompare
I tried it locally and the string lengths are different. According to your needs, you can try the following method to compare