Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

323
Vistas
encode ASCII symbols into UTF-8 presentation

I have a string that I k'now for sure has only ASCII lettes. JS treats strings as UTF-8 by default, so it means that every character takes up to 4 bytes, which is 4 times ASCII.

I'm trying to compress / save spaces / get the shortest string as possible, by having an encode and decode functions.

I thought about representing 4 characters of ASCII on a UTF-8 string and by that achieve my goals, is there anything like that? UTS-8 compressed to ASCII

If not, what is the best way to compress ASCII strings, so that by encoding and decoding I'll reach the same string?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Actually JavaScript encodes program strings in UTF-16, which uses 2 octets (16 bits) for Unicode characters in the BMP (Basic Multilingual Plane) and 4 octets (32 bits) for characters outside it. So internally at least, ASCII characters use 2 bytes.

There is room to pack two ASCII characters into 16 bits since they only use 7 bits each. Furthermore, since the difference between 2**16 and 2**14 is 49152, and the number of encodings used by surrogate pairs in UTF-16 is (allegedly) 2048, you should be able to devise an encoding scheme that avoids the range of code points used by surrogates.

You could also use 8 bit typed arrays to hold ASCII characters while avoiding the complexity of a custom compression algorithm.

The purpose of compressing 7 bit ASCII for use within JavaScript is largely (entirely?) academic these days and not something there is a demand for. Note that encoding 7 bit ASCII content into UTF-8 (for transmission or file encoding) only uses one byte for ASCII characters due to the design of UTF-8.

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want to use 1 byte per character you can simply use a byte. There is already a function to change to a string from bytes.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda