1. cvs

Converts any object to its string representation.

1.1. Syntax

any string cvs → substring

1.2. Stack Effects

Table 1. Before
Level Object

1

any (object to convert)

0

string (buffer for result)

Table 2. After
Level Object

0

substring (portion of string used)

1.3. Description

cvs (convert to string) produces a text representation of an arbitrary object and stores it into the supplied string, overwriting an initial portion of its value. It returns a string object designating the substring actually used.

Conversion rules: * Numbers: Text representation (e.g., 123, 3.14) * Booleans: true or false * Strings: Contents copied verbatim * Names/operators: Text representation of name * Other types: --nostringval--

For real numbers, the exact format is implementation-dependent (e.g., 0.001 or 1.0E-3).

1.4. PostScript Level

Level 1 and later

1.5. Examples

Converting numbers
/str 20 string def
123 456 add str cvs
% Result: (579)
Converting booleans
/str 10 string def
true str cvs     % Result: (true)
false str cvs    % Result: (false)
Converting names
/str 20 string def
/MyName str cvs  % Result: (MyName)
Unconvertible types
/str 20 string def
mark str cvs     % Result: (--nostringval--)

1.6. Common Use Cases

1.6.1. Number to String Conversion

/num 42 def
20 string
num exch cvs
% Use string representation

1.6.2. Building Messages

/message 100 string def
(Value: ) message 0 7 getinterval cvs
value message 8 message length 8 sub getinterval cvs

1.6.3. Debug Output

/debug {
  20 string cvs print (\n) print
} def

123 debug        % Prints "123"
/name debug      % Prints "name"

1.7. Common Pitfalls

Buffer Must Be Large Enough - If string is too small, [rangecheck] error occurs.
123456 3 string cvs  % Error: need at least 6 chars
Real Number Format Varies - Format for reals is implementation-dependent.
0.001 20 string cvs  % Might be (0.001) or (1.0E-3)
Returns Substring - Result is a substring sharing the buffer, not a new string.
/buf 20 string def
123 buf cvs /s1 exch def
456 buf cvs /s2 exch def
% s1 now shows (456...) - buffer was reused!
Use Ample Buffer Size - Allocate at least 20-30 characters for number conversions.

1.8. Error Conditions

Error Condition

[invalidaccess]

String has no-access or read-only attribute

[rangecheck]

String too small for conversion result

[stackunderflow]

Fewer than 2 operands on stack

[typecheck]

Second operand is not a string

1.9. Implementation Notes

  • Numbers converted using PostScript syntax rules

  • String result shares the buffer (not a copy)

  • For strings, performs a copy operation

  • Return value points to used portion of buffer only

Object Type Min Size Reason

Integers

12

Max ~10 digits plus sign

Reals

30

Scientific notation possible

Names

Variable

Length of name + 1

Booleans

6

"true" or "false"

1.11. See Also

  • cvn - Convert string to name

  • cvi - Convert to integer

  • cvr - Convert to real

  • string - Create string buffer

  • type - Get object type


Back to top

Copyright © 2025 Ribose. PostScript is a trademark of Adobe. Distributed under the MIT License.