Reading and writing binary values

Reading and writing binary values
 1# Create a local variable for the active drawing
 2ad = impact.active_drawing
 3
 4ot = impact.gui.output_toolbox
 5
 6ot.clear()
 7
 8# Check that there is an active drawing
 9if not ad.isNone():
10
11    # Create a local variable for database values
12    database_values = ad.database_values
13
14    empty_bytes = database_values.load("D_MYPIC")
15
16    # Shows how to detect an empty value
17    if is_empty(empty_bytes):
18        ot.add("Current value IsEmpty")
19
20    bytes = [None] * 101
21
22    for i in range(0, len(bytes) - 1  + 1):
23        bytes[i] = int(i % 256)
24
25    # stick in an embedded None
26    bytes[10] = int(0)
27
28    ot.add("Saving: " + str(type(bytes)) + ", LBound: " + str(0) + ", UBound: " + str(len(bytes) - 1) + ", Length: " + str(len(bytes) - 1 - 0 + 1))
29
30    call(database_values.save("D_MYPIC", bytes))
31
32    bytes2 = database_values.load("D_MYPIC")
33
34    ot.add("Loading: " + str(type(bytes2)) + ", LBound: " + str(0) + ", UBound: " + str(len(bytes2) - 1) + ", Length: " + str(len(bytes2) - 1 - 0 + 1))
35
36    if (0 == 0 and len(bytes) - 1 == len(bytes2) - 1):
37        for i in range(0, len(bytes2) - 1  + 1):
38            if int(bytes[i]) != int(bytes2(i)):
39                ot.add("Byte " + str(i) + ": expected " + str(bytes[i]) + " received " + str(bytes2(i)))
40
41    else:
42        ot.add("Invalid bytes received - bounds error")
43
44else:
45    ot.add("Unable to continue: no active drawing")