Javascript compares strings of different encodings
三叔
三叔 2017-07-05 10:43:04
0
3
855

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

三叔
三叔

reply all(3)
淡淡烟草味

Javascript's === determines whether the strings are the same based on the unicode code point value

buf1.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 localeCompare

to determine whether the characters are the same.
'\u0000=-='.localeCompare('=-=\ufeff')
学霸
console.log(buf1.toString().localeCompare(buf2.toString('utf16le')) === 0)
滿天的星座

I tried it locally and the string lengths are different. According to your needs, you can try the following method to compare

const buf1 = Buffer.from('c2bf43c3b36d6f20657374c3a1733f', 'hex')
const buf2 = Buffer.from('fffebf004300f3006d006f002000650073007400e10073003f00', 'hex')
var b1=buf1.toString();
var b2=buf2.toString('utf16le');
console.log(b1.length)
console.log(b2.length)

console.log(b2.includes(b1));

//输出结果
// 12
// 13
// true
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template