Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

328
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda