麻烦在JavaScript上述字符code 128二进制字符串 [英] Trouble with binary string in javascript above character code 128

查看:416
本文介绍了麻烦在JavaScript上述字符code 128二进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想创建并保存一个二进制文件本地JavaScript中使用这个库:的https: //github.com/eligrey/FileSaver.js/

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

这是一个有点棘手,因为我使用的游戏制作工作室,我可以使用JavaScript的互动方式是有点有限,但这里是我的设置..
除了从GitHub库的实际filesaver.js,我的js code是这样的:

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;
}

基本上,这个想法是,我首先填充使用字符codeS创建一个二进制字符串,然后做成一个blob,并保存单个字节数组。这适用于低于128二进制值,而不是上述原因,可能是因为一些字符集问题..我该如何解决这个问题?

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?

例如,如果我尝试填补了阵列0-255递增,这是生成的二进制文件的内容,你可以经过128个字节中看到的字符突然创建两个字节的每一个,而不是一个,这一切都错误:

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是C 2 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 AF C3 C3 B0 C3 B1 B2 C3 C3 B3 C3 B4 B5 C3 C3 B6 C3 B7 B8 C3 C3 B9 C3 BA C3 BB C3 BC C3 BD的C3

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

推荐答案

而不是使用的字符串,你可能会更好使用 Uint8Array (即范围为0&ndash的无符号字节数组; 255);看的https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array.例如:

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"});

另外,顺便说一句— argument0 参数1 是可怕的可变名。你提到你被游戏制作的制约;就是这样做的原因?好像应该有某种方式来解决这个问题。

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.

这篇关于麻烦在JavaScript上述字符code 128二进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆