Google Groups Home
Help | Sign in
Howto print leading zeros when converting from number to string...?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Heady  
View profile
 More options 7 Mar, 00:12
From: Heady <adrianhead1...@googlemail.com>
Date: Thu, 6 Mar 2008 16:12:37 -0800 (PST)
Local: Fri 7 Mar 2008 00:12
Subject: Howto print leading zeros when converting from number to string...?
First off - apologies - as I'm new to Postscript and to
postscriptbarcode.  I've spent the last few days searching for a
solution to no avail.

I've written a small, quick and dirty postscript script to generate 40
asset labels in a matrix; with the postscriptbarcode generating a
barcode in the middle of the label.

So far by various means - I've been able to:
* generate a label outline and assign it as a form function (not sure
of correct name...)
* Generate a logo and assign it as a form function
* Write text to the appropriate spots in the label
* Convert a number to a string and concatenate with a string prefix
(which includes the ^104 Code 128 B identity digit)
* Get postscriptbarcode to generate a Code 128 B barcode for the above
concatenated string

However, apart from the Ghostscript errors which I'll try to sort out
latter - which are caused by my logo conversion (long story) that only
confuses Ghostscript and prolongs the time the printer processes the
page.  I cannot for the life of me - work out how to display leading
zeros in the above number converted to a string.

For example - I have the script counting from 1 to 40.  So I need the
string representation of the number to be 0000000001 instead of 1.  In
some languages you have printf operators but I cannot find anything
equivalent for postscript so far.  The quick solution I have so far is
to define the AssetNum as 1000000001 as this works - but it isn't as
neat as I'd like.  However, if it is too cumbersome then I might be
able to modify the original requirements to include the initial
position 1.

Can anyone suggest where I can go to find the appropriate
information?  Or does someone know where I could find a suitable
function?  If there is a postscript operator that I've missed - then
please point me in the correct direction and apologies in advance.

My small, quick and dirty code looks something like this:

....8<...........

% Routine from http://www.tinaja.com/glib/strconv.pdf
/mergestr {
        2 copy length exch length add string dup dup
        4 3 roll 4 index length exch putinterval 3 1 roll exch
        0 exch putinterval
        }
def

/AssetNum 1 def

1 1 10 {
        1 1 4 {
                1 1 scale

                LabelOutline execform

                /Helvetica findfont 15 scalefont setfont
                78.5 770 moveto (Asset) show
                /Helvetica findfont 8.5 scalefont setfont
                84 759 moveto (Training Centre) show
                /Helvetica findfont 4 scalefont setfont
                153.5 719 moveto (v0.1) show

                /barcodetxt (^104UK) AssetNum 10 string cvs mergestr def

                gsave
                47 732 translate
                0.6 0.6 scale
                0 0 moveto barcodetxt (includetext height=0.5  textsize=16
textxalign=center textfont=Helvetica textxoffset=-1 textyoffset=2)
code128 barcode
                grestore

                Logo execform

                /AssetNum AssetNum 1 add def
                129 0 translate
        } for
        -516 -72.5 translate

} for

Thanks in advance

Heady


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Burton  
View profile
 More options 7 Mar, 01:06
From: "Terry Burton" <t...@terryburton.co.uk>
Date: Fri, 7 Mar 2008 01:06:41 +0000
Local: Fri 7 Mar 2008 01:06
Subject: Re: [postscriptbarcode] Howto print leading zeros when converting from number to string...?
On Fri, Mar 7, 2008 at 12:12 AM, Heady <adrianhead1...@googlemail.com> wrote:
>  I've written a small, quick and dirty postscript script to generate 40
>  asset labels in a matrix; with the postscriptbarcode generating a
>  barcode in the middle of the label.

>  So far by various means - I've been able to:
>  * generate a label outline and assign it as a form function (not sure
>  of correct name...)
>  * Generate a logo and assign it as a form function

<...snip...>

Hi Heady,

It's nice to see an example use of PostScript forms on this list -
thanks for including the code.

If you are willing and happy to do so, would you be prepared to send a
version of your finished script that is suitable for including in the
example scripts section of the repository?
http://postscriptbarcode.googlecode.com/svn/scripts/

>  However, apart from the Ghostscript errors which I'll try to sort out
>  latter - which are caused by my logo conversion (long story) that only
>  confuses Ghostscript and prolongs the time the printer processes the
>  page.  I cannot for the life of me - work out how to display leading
>  zeros in the above number converted to a string.

In BWIPP I use the following inline mantra whenever I need to do
something similar to what you need:

(000000000000) 12 string copy dup NUM 10 12 string cvrs dup length 12
exch sub exch putinterval

This is an efficient technique that does not require the use of
ephemeral variables - obviously you need to replace the string of
zeros and the instances of "12" with the appropriate level of padding.

It is easier to understand when written as follows:

(000000000000)  % Define the padding field
12 string copy dup  % Persist a new working instance of this into
which we will insert the number
NUM 10 12 string cvrs  % Convert the given NUM into the equivalent
base 10 string, up to the length of the field width
dup length  % Find the actual length of the string of NUM
12 exch sub  % Subtract this length from the field width to find the
required level of padding, the offset
exch putinterval  % Place the string into the working instance of the
field at this offset position

It is fairly easy to wrap this into a function, but I'll leave that as
an exercise for the reader ;-)

Hope this helps,

Tez


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Heady  
View profile
 More options 8 Mar, 18:56
From: Heady <adrianhead1...@googlemail.com>
Date: Sat, 8 Mar 2008 10:56:21 -0800 (PST)
Local: Sat 8 Mar 2008 18:56
Subject: Re: Howto print leading zeros when converting from number to string...?
Thanks Tez

Well the result of the reader exercise was:

% Routine from http://groups.google.com/group/postscriptbarcode/
% Convert num to str and pad leading zeros: num num2str -> (000000num)
% Note: must change (00..00) & 10 for different str length
/num2padstr {
        (0000000000) 10 string copy dup
        3 2 roll 10 10 string cvrs dup length
        10 exch sub exch putinterval
        }
def

And can be called by:

/barcodetxt (text) AssetNum num2padstr mergestr def

Heady

On Mar 7, 1:06 am, "Terry Burton" <t...@terryburton.co.uk> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Heady  
View profile
 More options 8 Mar, 19:35
From: Heady <adrianhead1...@googlemail.com>
Date: Sat, 8 Mar 2008 11:35:20 -0800 (PST)
Local: Sat 8 Mar 2008 19:35
Subject: Re: Howto print leading zeros when converting from number to string...?
My final solution - also allows for easy modification for dec or
hex...

% Routine from http://groups.google.com/group/postscriptbarcode/
% Convert num to str and pad leading zeros: num num2padstr ->
(0000num)
% Note: must change (00..00) & 10 for different str length
% Note: numbase:  10 for dec; 16 for hex
/num2padstr {
        /numbase 16 def
        (0000000000) 10 string copy dup
        3 2 roll numbase 10 string cvrs dup length
        10 exch sub exch putinterval
        }
def

Called by:

/AssetNum 1 def
/barcodetxt (^104txt) AssetNum num2padstr mergestr def
0 0 moveto barcodetxt (includetext height=0.5  textsize=16
textxalign=center textfont=Helvetica textxoffset=-1 textyoffset=2)
code128 barcode

Heady


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google