0

So I'm trying to create and save a binary file locally in javascript using this library: https://github.com/eligrey/FileSaver.js/

It is a bit tricky because I am using GameMaker studio, and the ways I can interact with the javascript is a bit limited, but here's my setup.. Other than the actual filesaver.js from the github repo, my js code is this:

var ildablob = new Array();

toArray = function(argument0, argument1)
{
ildablob[argument0] = String.fromCharCode(argument1);
return 1;
}

save = function(argument0)
{
var blob = new Blob(ildablob, {type: "application/octet-stream"});
saveAs(blob, argument0);
return 1;
}

Basically, the idea is that I first fill up an array with individual bytes using character codes to create a binary string, which is then made into a blob and saved. This works for binary values below 128, but not above, probably because of some charset issue.. How can I fix this?

For example, if I try filling up the array incrementing from 0-255, this is the content of the resulting binary file, as you can see after 128 bytes the characters suddenly create two bytes each, not one, and it's all wrong:

00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F C2 80 C2 81 C2 82 C2 83 C2 84 C2 85 C2 86 C2 87 C2 88 C2 89 C2 8A C2 8B C2 8C C2 8D C2 8E C2 8F C2 90 C2 91 C2 92 C2 93 C2 94 C2 95 C2 96 C2 97 C2 98 C2 99 C2 9A C2 9B C2 9C C2 9D C2 9E C2 9F C2 A0 C2 A1 C2 A2 C2 A3 C2 A4 C2 A5 C2 A6 C2 A7 C2 A8 C2 A9 C2 AA C2 AB C2 AC C2 AD C2 AE C2 AF C2 B0 C2 B1 C2 B2 C2 B3 C2 B4 C2 B5 C2 B6 C2 B7 C2 B8 C2 B9 C2 BA C2 BB C2 BC C2 BD C2 BE C2 BF C3 80 C3 81 C3 82 C3 83 C3 84 C3 85 C3 86 C3 87 C3 88 C3 89 C3 8A C3 8B C3 8C C3 8D C3 8E C3 8F C3 90 C3 91 C3 92 C3 93 C3 94 C3 95 C3 96 C3 97 C3 98 C3 99 C3 9A C3 9B C3 9C C3 9D C3 9E C3 9F C3 A0 C3 A1 C3 A2 C3 A3 C3 A4 C3 A5 C3 A6 C3 A7 C3 A8 C3 A9 C3 AA C3 AB C3 AC C3 AD C3 AE C3 AF C3 B0 C3 B1 C3 B2 C3 B3 C3 B4 C3 B5 C3 B6 C3 B7 C3 B8 C3 B9 C3 BA C3 BB C3 BC C3 BD C3 BE

1 Answer 1

2

Instead of using a String, you'd probably be better off using a Uint8Array (meaning an array of unsigned bytes in the range 0–255); see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array. For example:

ildablob[argument0] = new Uint8Array(1);
ildablob[argument0][0] = argument1;

...

var blob = new Blob(ildablob, {type: "application/octet-stream"});

Also, incidentally — argument0 and argument1 are terrible variable-names. You mention that you're constrained by GameMaker; is that the reason for this? It seems like there should be some way to fix that.

Sign up to request clarification or add additional context in comments.

1 Comment

Wonderful, thank you! Yes, the argument names are related to Gamemaker, but on second thought I think they don't need to be that so I will change it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.