Is there a way to transform Unicode characters into their corresponding Unicode codepoints in hexadecimal format?

Overview

I've been grappling with a challenge by utilizing an array that encompasses the Unicode code point table. However, due to its immense size, I am encountering memory-related errors.

Breakdown of my Progress

Using Typescript alongside a Micro:Bit device, I have established a substantial array (consisting of 128 elements) crucial for my project. Within this array are diverse hexadecimal representations derived from the unicode table:

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

Nevertheless, when attempting to store this array on the Micro:bit, it leads to a 021 error message (indicating insufficient free memory or excess objects in GC). Are there any overlooked alternatives for storing this array effectively? Further along in my project, I necessitate using this array as a reference table to convert characters from a string into their respective Unicode equivalents:

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

Any strategies or recommendations to address this memory predicament? Alternatively, is there a more efficient approach to accomplish my goal outlined in the question title?

Answer №1

I have a strong hunch that you may be dealing with what is known as the XY Problem, and that your true inquiry should be:

"How can I efficiently transform characters into their Unicode codepoints?"

If my suspicion holds, here is the remedy. Update 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 modern ES6 method on string. It will work in Typescript if your tsconfig specifies ES2015 (alias ES6) as the value for target.

If upgrading to ES6 or beyond is not an option for you, then you can utilize the Polyfill provided in the MDN article on codePointAt by incorporating the function into your code.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to predict the completion time of the initial build when using the "--watch" option for TypeScript compilation?

Simultaneously building Tokens and ModulesCollection can occur in parallel while the Lexer will build once Tokens and ModulesCollection are built for the first time. The following code snippet contains an error due to the use of & in the line npm run & ...

Choosing custom element children once they're connected to the DOM: A guide

My goal is to retrieve the value of transaction-name__inputbox when the user clicks on the transaction-add__button. The function transactionAddHandler is triggered upon clicking the button. However, my attempt to select this element using document.querySe ...

Divide a string using multiple delimiters just one time

Having trouble splitting a string with various delimiters just once? It can be tricky! For instance: test/date-2020-02-10Xinfo My goal is to create an array like this: [test,Date,2020-02-10,info] I've experimented with different approaches, such ...

Tips for accessing and adjusting an ngModel that is populated by an attribute assigned via ngFor

Looking for guidance on how to modify an input with ngModel attribute derived from ngFor, and update its value in the component. Here is my code snippet for reference: HTML FRONT The goal here is to adjust [(ngModel)] = "item.days" based on button click ...

Is it necessary for me to validate the type before making each database request?

Currently, I am in the process of honing my error-handling skills within Express while using a Postgres database. My backend code consists of numerous small functions that serve as intermediaries for handling interactions with the database in larger funct ...

Is it possible to select all options in an Angular mat-select component using a Boolean function

Is there a way to enable Select All functionality in Angular mat-select by using Boolean functions without the need for form tags or form builders? Component.html <div layout="column" class="mat-cloumn w-25"> <mat-form-field ...

Template does not recognize changes in *ngIf

Currently, I am facing an issue where I want to display a loading message for 3 seconds and then hide it using the *ngIf directive in Angular 8. Interestingly, I have not encountered this problem in my previous projects. Below is the code snippet that I a ...

What is the best way to merge an array into a single object?

I have an array object structured like this. [ { "name": "name1", "type": "type1", "car": "car1", "speed": 1 }, { "name": &q ...

Is there a way to turn off tsc pretty printing using the configuration file?

My typescript program is intentionally broken but I want to fix it. 12:17:23:~/hello $ cat hello.ts console.log("Hello World" 12:17:29:~/hello $ cat package.json { "dependencies": { "typescript": "^5.2.2" ...

Using React.useContext in a Class Component with Typescript: Issue resolving the signature of the class decorator

Struggling to implement a store using React.createContext within my class components. The setup of my App is as follows: const someStore = new SomeStore(); export const StoreContext = React.createContext(someStore); export interface IProps { store ...

Accessing specific elements in Typescript 3.5 using indexes

I am looking to create a type-safe getter function that has the ability to return modified values while still being sound. class Foo { a: number; b: boolean; } function getProp<K extends keyof Foo>(o: Foo, p: K): Foo[K] { switch (p) { ca ...

Anticipate that the function parameter will correspond to a key within an object containing variable properties

As I develop a multi-language application, my goal is to create a strict and simple typing system. The code that I am currently using is as follows: //=== Inside my Hook: ===// interface ITranslation { [key:string]:[string, string] } const useTranslato ...

What is the best way to assign ngModel to dynamically inserted input rows in Angular 4+ (specifically in a mat-table)?

Just a quick question - how can I add ngModel to dynamically added new input rows? I have a Mat table with multiple rows and an "add element" method that adds a new row every time a button is clicked. This way, I want to bind the user-entered values and se ...

Adjusting the width of row items in Angular by modifying the CSS styles

I am envisioning a horizontal bar with items that are all the same width and evenly spaced apart. They can expand vertically as needed. Check out the updated version here on StackBlitz https://i.sstatic.net/MFfXd.png Issue: I am struggling to automatica ...

The 'DOCUMENT' module (imported as 'i23') could not be located within '@angular/platform-browser'

During my upgrade from Angular version 7 to 8, I encountered an error when building the project even though I am not using DOCUMENT. It seems like something is causing this issue that I am overlooking. I have thoroughly checked all the files and confirmed ...

Is it acceptable to manipulate the prevState parameter of the setState function as mutable?

It is commonly known that directly modifying this.state is not recommended, and instead setState should be used. Following this logic, I assumed that prevState should also be treated as immutable, and setState should always involve creating a new object i ...

The variable type does not align with the export type

My TypeScript project includes a file that loads environment variables and exports them: const.ts: const { VARIABLE0, // type of VARIABLE0 is string | undefined VARIABLE1, } = process.env; if (!VARIABLE0 || !VARIABLE1) { throw new Error('Inval ...

Gulp is failing to create a JavaScript file from TypeScript

Below is the Gulp task I am using: var gulp = require('gulp'); var typescript = require('gulp-typescript'); var sourcemaps = require('gulp-sourcemaps'); var tsProject = typescript.createProject('tsconfig.json'); var ...

"Access to the DOMRouterOpts interface is restricted and cannot be

Encountering a problem with the import type issue while using react-router-dom:6.22.3 alongside TypeScript in a React project. import { Route, createBrowserRouter, createRoutesFromElements } from "react-router-dom"; import type { RouteObject } fr ...

Connecting an Angular 4 Module to an Angular 4 application seems to be causing some issues. The error message "Unexpected value 'TestModule' imported by the module 'AppModule'. Please add a @NgModule annotation" is

Update at the bottom: I am currently facing a massive challenge in converting my extensive Angular 1.6 app to Angular 4.0. The process has turned into quite a formidable task, and I seem to be stuck at a specific point. We have a shared set of utilities th ...