How can I convert Unicode characters to their Unicode codepoint in hex format?

687 views Asked by At

Summary

I've been trying so solve this via an array that holds the Unicode codepoint table, but it is so large I am getting memory errors.

Details of my effort so far

I am using Typescript in combination with a Micro:Bit. I have a large array (128 entries) that is necessary for my program. This array stores various hex representations of the unicode table:

let font: number[] = [0x000b6526, 0x010514bf, 0x0004d6b2, 0x0010fc21, 0x0007c20f,
  0x00744107, 0x01f4111f, 0x000d909b, 0x00117041, 0x0008ceb9, 0x0008c7e0, 0x01041041, .......];

However, storing this array on the Micro:bit results in an 021 error (No free memory or too many objects in GC). Is there any alternative that I have missed to store this array? Further on in my program I need to use this array as a lookup table to convert chars of a string into their corresponding unicode characters:

let character = font[string.charCodeAt(stringPosition)]

Any ideas or suggestions on how to solve this memory issue, or is there a better way to achieve my aim as stated in the title of this question?

1

There are 1 answers

0
Inigo On

I highly suspect this is a case of the XY Problem, and that your real question is:

"How can I efficiently convert characters to their Unicode codepoint?"

If I am right, here is your solution. Replace your array and lookup with:

let s = 'a  right now would be nice.'
let stringPosition = 2 // third char
let character = s.codePointAt(stringPosition)  // 129347, the secret code for a stiff drink

codePointAt is a new ES6 method on string. It will be available in Typescript if your tsconfig has ES2015 (aka ES6) as the value for target.

If you are unable to switch to ES6 or later, then you can use the Polyfill given in the MDN article on codePointAt by copying the function to your code.