# Converting arrays to numbers

I have a 512x512 boolean 2D array that I would like for the user to save the state of each element to a file.
This file is a text file as it also contains other information.
My first attempt was to generate a string of 1s and 0s representing the state of each element in the array
I then attempted to convert this string to a number by the following code
for i=1 to String.Len
factor=CDbl(mid(String,i,1))//either 1 or 0
exponent=Len(String)-i
number = number +factor*pow(2,exponent)
next
Then I could just save the value.
I could write code to convert this number back to the 512x512 array when the user loaded the file back into the program.

As you might have guessed, 2^262144 exceeds the maximum allowable size of a double.
Is there a number type that can handle values of this size?
Is there any other way to do this aside from saving a 262144-long string?

a memoryblock should do the trick, or a string but it will use 8 times more memory

You could even stuff it into Bits. You then would then have a 32k file.

[quote=290807:@JOSEPH CALABRIA]As you might have guessed, 2^262144 exceeds the maximum allowable size of a double.
Is there a number type that can handle values of this size?
Is there any other way to do this aside from saving a 262144-long string?

[/quote]

You may want to try Bob Delaney’s Decimal Plugin http://delaneyrm.com/DecimalPlugin.html

Is file size a concern?

[quote]This file is a text file …
My first attempt was to generate a string of 1s and 0s[/quote]

That will work perfectly well as storage.
It will make the file 256Kb in size, but you dont need to do anything clever with it.

“01001001110010010001001… etc” 512 characters long
512 rows of this

Read it back into a 512 string array or (probably better) a 512 element array of memoryblocks.
(You can use the .byte method of a memoryblock to get and set an individual value once in memory)

Actually, it just occurred to me that if you want an easier to address array, you could do worse than a picture

dim p as picture
p = new picture(512,512,1)
p.Graphics.pixel (2,2) = &c000000 //wrap this up with an access method so that you get and set true and false, but store black and white

for example, the following generates a 6Kb file:

``` dim p as picture p = new picture(512,512,1) p.Graphics.pixel (2,2) = &c000000 dim f as FolderItem f= specialfolder.desktop.child("test.png") p.Save (f,picture.saveaspng)```

Storing numeric data as picture can be a mess if you use compressed formats.

Michel is right, although its my understanding that PNG is lossless compression.

It is a good idea, but it merits thorough verification before implementing it in an application.