Writing UInt8 data to a file

Hello friends,

First and foremost I just want to mention that I love Scriptable - it’s fantastic being able to write programs in JS on my iPhone.

I’m a complete noob to JS, but thanks to Scriptable, Emscripten and Browserify I’ve just about finished porting nesasm, a 6502 assembler, to my iPhone.

I can’t seem to figure out how to write the assembled data to a binary file, however. I have an array of UInt8’s, and I’d like to write those bytes to a binary file, but whenever I try to write with FileManager’s write method it gives me the error:

Expected value of type Data but got value of type {string:number}

I’ve tried different things like using TypedArrays to try and coerce the data into Data, but to no avail. Is there a way to do this?

Thanks for your help!

1 Like

You can create a Data object with some of these methods: https://docs.scriptable.app/data

Your best bet will be to encode your uint8 array as base64 and then use the Data.fromBase64String(str) method to get the Data object.

It seems that there is an instance function to get the byte array from a Data object, but not the other way around… @simonbs is this intentional?

Thank you so much for the reply. I took your advice and after converting into base64 it works as expected. In my 38 years on Earth this is the first time I can recall actually asking for advice online, really appreciate that you helped make it a positive one.

It would make sense if there was an API to create a Data object form a byte array. The Data API is currently rather limited mostly because the demand for expanding it haven’t been really been there and it was only put in the world to solve very specific challenges.

I’m very open to expanding the Data API in the future :blush:

1 Like

That’s awesome. Stuff like that could potentially expand what kind of use cases people use the app for (like creating assemblers for iOS), but I’m sure you have all kinds of priorities to sort through, so for now I’ll just say thanks for the wonderful app.