0

When i run the punycode.ucs2.decode

("This is a message with emoji ⚽ ⛄ ) ")

I'm obtaining numbers greater than 0xFFFF (65536) in codes. That causes me a problem because i need to convert this into a paddedHexString. So i have to pad the string to 6 digits when i am receiving that emojis.

It looks like it is happening with secondary set of emojis in the document.

http://www.unicode.org/Public/emoji/1.0/emoji-data.txt

Function that breaks

function toPaddedHexString(num, len) {
        const str = num.toString(16).toUpperCase();
        return "0".repeat(len - str.length) + str;
 }
Omar Alvarado
  • 1,304
  • 2
  • 12
  • 16
  • What should `punycode.ucs2.decode("")` return? `decode` returns an array of code points. – Blender Apr 11 '18 at 01:08
  • Yes, in the array i'm getting numbers greater than 65536, that causes that i get 1F60A instead of D83DDE0A that was surrogated. – Omar Alvarado Apr 11 '18 at 01:15
  • But the codepoint of `` is 128525, so `decode` works properly. Surrogate pairs are used only by some encodings. How are you using the these code points? – Blender Apr 11 '18 at 01:18
  • What i need is a padded string of 4 digits each but if it is greater the second script in my edit breaks... – Omar Alvarado Apr 11 '18 at 01:28
  • What is this code supposed to do though? What encoding are you trying to implement? – Blender Apr 11 '18 at 01:35
  • I think i was confused of what to do. I am trying to build a binary text field with UCS2 encoding, but provider asked me to have each letter in 4 hex digits, so the function toPaddedHexString allows me to do that, but when i receive some emojis it breaks because. Another possibility is only to pad all letters to 6 hex digits, but in a message of 100 chars it converts to an string with 600 of length, to i think is better to surrogate that characters to keep the length as short as possible – Omar Alvarado Apr 11 '18 at 15:43
  • After look into it, i noticed that the problem isn't at punycode.js but at the implementation of the algorithm to convert the text... – Omar Alvarado Apr 11 '18 at 15:49

0 Answers0