Project

General

Profile

encode_binary_data_input.p

Greg Shah, 11/04/2014 03:02 PM

Download (1.32 KB)

 
1
def var txt    as char.
2
def var e-txt  as char  format "x(16)".
3
def var i-file as char   init "binary_string_input.txt".
4
def var o-file as char   init "encoded.txt".
5
def var sz     as int.
6
def var idx    as int.
7
def var i      as int.
8
def var remain as int.
9
def var ptr    as memptr.
10

    
11
def stream out.
12
output stream out to value(o-file).
13

    
14
file-information:filename = i-file.
15
remain = file-information:file-size.
16

    
17
set-size(ptr) = 1024.
18
idx = 1.
19

    
20
do while (idx <= remain):
21
   /* read 1 byte that tells us the size of the following binary string */
22
   copy-lob from file i-file starting at idx for 1 to ptr.
23

    
24
   sz = get-byte(ptr, 1).
25
   idx = idx + 1.
26
   
27
   /* empty string is size 0 */
28
   if sz > 0 then
29
   do:
30
      /* read in the "string" which may have null chars */
31
      copy-lob from file i-file starting at idx for sz to ptr.
32
   end.
33

    
34
   idx = idx + sz.
35
   
36
   /* this is the only known way to get a null char into a character var! */
37
   txt = get-string(ptr, 1, sz).
38
   
39
   if length(txt) ne sz then message "PROBLEM!" length(txt) sz. 
40

    
41
   /* chr(0) returns empty string so this is not the best approach */
42
   /*
43
   txt = "".
44
   do i = 1 to sz:
45
      txt = txt + chr(get-byte(ptr, i)).
46
   end.
47
   */
48
   
49
   e-txt = encode(txt).
50
   
51
   put stream out unformatted e-txt skip.
52
end.
53

    
54
output stream out close.
55

    
56
/* de-allocate memory */
57
set-size(ptr) = 0.