0

Conversely, how do I get "0031" from "1"; or "5185" from "内".

I know you can use codePointAt() to get the unique HTML entity code (in the case of A, it returns "65"), but I don't seem to find a way to bring out the other value, which I don't even know the name of.

It's the value shown here or here at the "UTF-16 Encoding" row

  • No, `.codePointAt()` returns a number, not an HTML entity. – Pointy Jul 05 '21 at 16:33
  • 2
    `char.codePointAt(0).toString(16).padStart(4, "0")` – VLAZ Jul 05 '21 at 16:35
  • @Pointy Sorry I was wrong in my terminology, I know it returns a number, but it's not the number I'm looking for. My problem is that I don't know the terms for the Unicode codes. A quick example I added to my post was using codePointAt() with "A" returns the number "65", but what I need is "0041" or at least "41". – ARandomControl Jul 05 '21 at 16:37
  • @VLAZ Thank you, that returns the number I'm looking for. Can you tell me the terminology or names of these codes for future reference? – ARandomControl Jul 05 '21 at 16:41
  • If you want the hex value, you can do `"A".codePointAt(0).toString(16)` - but that will be a *string*, not a number. The number 65 (base 10) is precisely identical to the value 41 in base 16. – Pointy Jul 05 '21 at 16:42
  • @ARandomControl it's the UTF code point. It's in hex - the `codePointAt` returns an integer in base 10, so `toString(16)` converts it to hex. The `65` (base 10) for `"A"` thus turns to `31` (base 16). UTF-16 code points consist of four hex digits. That's why you can pad the beginning and `31` becomes `0031`. – VLAZ Jul 05 '21 at 16:44

0 Answers0