1

This is an example file viewed in a hex editor: link I have a binary file on my computer that I want to upload and store as a JavaScript array. It doesn't matter if the final array is

thebytes = [0x00, 0x43, 0x41, 0x46, 0x45, 0x42, 0x41, 0x42, 0x45, 0xFF]

or

thebytes = [0, 67, 65, 70, 69, 66, 65, 66, 69, 255]

but it can't be any kind of ASCII or Unicode representation, I simply want the file's byte values to an array.

I've found numerous guides but they all either concentrate on text files or Strings generally or are ~5 years old and imply that it isn't possible with JS.

Related: a Codepen I'm trying to implement it into. The first variable is "thebytes" and contains a boatload of bytes, manually formatted and inserted. This is what I'm trying to avert - I just need a method of pushing "Choose file" and "OK" so I don't have to jump between a hex editor, a text editor and Codepen.

Thanks.

  • So you want to use a local file on client without uploading it to server? If so, [this article](https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications) may help you. – hindmost Dec 08 '15 at 08:09
  • Really doesn't matter, I'm doing everything on Codepen (which to my knowledge allows temporary uploads to server) and I don't intend on moving the project anywhere else like a home server or Github. @hindmost thanks, I'll look into it – Verneri Kontto Dec 08 '15 at 08:13
  • Why you dont use the hex format to send data? I think that the best way to send byte array via ajax and xml or json format is converting it to hex format. – Pedram Dec 08 '15 at 08:13
  • Because it's very tedious to first open a file in a hex editor, copy all the bytes, paste them into a text editor, format them as a Javascript array, and paste them into Codepen. – Verneri Kontto Dec 08 '15 at 08:15
  • Take a look at this: http://stackoverflow.com/questions/14603205/how-to-convert-hex-string-into-a-bytes-array-and-a-bytes-array-in-the-hex-strin – Pedram Dec 08 '15 at 08:23
  • Thanks @pedram , that's very useful. – Verneri Kontto Dec 08 '15 at 08:27

0 Answers0