Converting  Between  PostScript
Strings,  Integers,  and  Arrays
By Don Lancaster                                                                    
Version 2.1 April 5, 1997
Copyright c. 1997 by Don Lancaster and Synergetics, Box 809, Thatcher AZ, 85552
(520) 428-4073. synergetics@tinaja.com All commercial rights and all electronic media
rights are *fully* reserved. Linking welcome. Reposting is expressly forbidden.

Further support on http://www.tinaja.com
Consulting services available via don@tinaja.com


Deep down inside, a PostScript string is really a group of numbers. Sometimes you want
the string to represent ASCII characters. Other times you want the string to represent a
group of integers of value decimal 0-155 or hex $00 to $FF.

A PostScript string can thus be an extremely compact way to stash 8-bit data.

Here are some elegant constructs to get from string-as-characters to string-as-numbers
and vice versa.  Note that exceptionally long strings can cause stack overflows.

String to integers -  This code converts a string on the top of the stack to a series of
                                 integers on the stack...  

                                                    (yourstring) {} forall      

Sring to array -       This code converts a string on top of the stack to an array on top
                                of the stack...

                                            (yourstring) mark exch {} forall ]

Array to string -      The stack top array is only allowed to contain integers in the range
                                 of decimal 0 to 255 or hex $00 to $FF...

                                   /makestring { dup length string               % new string of right size                                                          /NullEncode filter             % make a file out of string
                                                        2 index                             % array to stack top
                                                        { 2 copy write pop }         % integers to string
                                                        forall
                                                        pop exch pop                   % clean up
                                                        } bind def

                                         [yourarray] makestring                     % use it like so

                     Many thanks to Marcel Weiher for contributing this superb routine.

Integers to array -  Convert the integers to a stack array...

                                    int0 int1 int2 .... intN  intcount array astore

Integers to string -  Convert the integers to an array first using the above code. Then...

                                    int0 int1 int2 .... intN  intcount array astore makestring


Array to integers - Use these plain old PostScript instructions...

                                        [ yourarray ]   aload pop


Copyright c. 1997 by Don Lancaster and Synergetics, Box 809, Thatcher AZ, 85552
(520) 428-4073. synergetics@tinaja.com All commercial rights and all electronic media
rights are *fully* reserved. Linking welcome. Reposting is expressly forbidden.

Further support on http://www.tinaja.com
Consulting services available via don@tinaja.com