7

I have an application that loads user text through a XMLHttpRequest and returns it in a binary format. I understand that the main difference between 8, 16, and 32 is the amount of bytes per element but I don't know when to use each.

For example, for my application, since the text file could contain any possible characters really, which one would be best?

I have tried different files including one that that had emojis and it seemed that in the Uint8Array the emoji took up 4 indices.

Is there any reason that for my use I shouldn't just use the Uint8Array or is there a reason I should dynamically choose when the file is read? I have read the MDN docs on each but it doesn't seem to offer much insight other than byte size.

Here is the code I currently use to load the file:

asset= new XMLHttpRequest();

asset.addEventListener('readystatechange', function load() {

    if (asset.readyState == 4 && asset.data.status == 200) {

        const arrayBuffer = asset.data.response;

        if (arrayBuffer) asset.data = new Uint8Array(arrayBuffer);

    }

}.bind(this), false);

// Error handling.

asset.responseType = 'arraybuffer';

asset.open('GET', asset.src);

asset.send();
user3382203
  • 169
  • 1
  • 6
  • 25
Mr.Smithyyy
  • 2,157
  • 12
  • 49
  • 95
  • What exactly is the source "binary format"? You describe it as "text" ... What I mean is, UTF-8 could be described as a "binary format" because ultimately *everything* in computing involves binary data. – Pointy Jan 17 '19 at 13:31
  • @Pointy The best way I could describe it was as "text" because I really don't know what could be in the text file, the only thing I have to do is return the "binary format" as either `Uint8Array`, `Uint16Array`, or `Uint32Array`. – Mr.Smithyyy Jan 17 '19 at 13:44

1 Answers1

1

It all depends on what type of data you are sending into it. 8 bit is fine for ASCII characters and the like. 16 bit is for UCS-2. 32 bit is for UTF-32, which you wouldn't necessarily use because it's more for edge cases. Windows use of this is barely existant and Unix only uses it in internal applications sometimes.

Scott Craig
  • 275
  • 2
  • 10
  • 2
    I sort of understand but for example emojis aren't part of the ASCII character set but they can still be used just fine with `Uint8Array` they just take up 4 slots in the array. The data I'm sending to it can be anything that can be placed inside of a text file by the user, there is no restriction. – Mr.Smithyyy Jan 17 '19 at 13:42