search

Home  >  Q&A  >  body text

Javascript compares strings of different encodings

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

三叔三叔2769 days ago884

reply all(3)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-07-05 10:45:04

    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')

    reply
    0
  • 欧阳克

    欧阳克2017-07-05 10:45:04

    console.log(buf1.toString().localeCompare(buf2.toString('utf16le')) === 0)

    reply
    0
  • 滿天的星座

    滿天的星座2017-07-05 10:45:04

    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
    

    reply
    0
  • Cancelreply