Saving binary files
You've already learned that Processing can save data to a text file. In this recipe, we'll take a look at how you can write data to a binary file. This might be useful when you want to create your own proprietary file format.
How to do it...
You need to declare an integer array with a length of 1000 before the setup()
function. When you run the sketch, this array will be filled with some random numbers representing uppercase and lowercase letters of the alphabet.
int[] numbers = new int[1000]; void setup() { for ( int i = 0; i < numbers.length; i++ ) { if ( random( 100 ) < 50 ) { // uppercase A - Z numbers[i] = floor( random( 65, 91 ) ); } else { // lowercase a - z numbers[i] = floor( random( 97, 123 ) ); } } }
Inside the draw()
function, we'll convert the integer array into a byte array and use the saveBytes()
method to save the data to the hard drive.
void draw() { if ( keyPressed ) { byte[] bytes = byte( numbers )...