Lessons

String Manipulation: Conversions

Welcome to our lesson on string manipulation focusing on conversions in AutoLISP! The ability to convert data from one type to another is a fundamental skill in any programming language. In this lesson, we’ll delve into specific functions that allow us to transition between numbers and strings, angles and strings, and more. Understanding these conversion functions will drastically expand your toolkit, enabling you to tackle a broader range of challenges in CAD design and beyond.


Learning Outcomes

By the end of this lesson, students will be able to:

  • Identify and understand the core string conversion functions in AutoLISP.
  • Implement these functions to convert between different data types effectively.
  • Analyze and interpret the output from these conversion functions.
  • Incorporate these conversion tools in larger AutoLISP routines, enhancing the versatility of their scripts.

rtos

Definition: Converts a real number to a string, with optional formatting.

Syntax:

(rtos number [mode [precision]])

Example 1:

(rtos 3.14159265)
; This will return “3.14159265”

This function converts the real number 3.14159265 to a string without any specific formatting.

Example 2:

(rtos 3.14159265 2 2)
; This will return “3.14”

Here, the number is formatted as a decimal with two decimal places.

Example 3:

Using the mode parameter to represent the number in engineering format with a specified precision.

(rtos 3141.59265 3 3)
; This will return “3.142E+03”

In this example, the real number 3141.59265 is converted to a string using the engineering format (mode 3). The output represents the number with three decimal places, thus rounding the number to “3.142E+03”.

Example 4:

Converting the number to architectural format with a specific precision.

(rtos 12.75 4 2)
; This will return “12′-9″”

Here, the real number 12.75 is transformed into an architectural format (mode 4). The number 12.75 is interpreted as 12 feet and 9 inches. The precision parameter specifies that the inch value should be rounded to the nearest whole number, so the function returns “12′-9″”.


angtof

Definition: Converts an angle string to a real number.

Syntax:

(angtof string [mode])

Example 1:

(angtof “45d”)
; This will return 0.785398

This converts the angle “45d” (45 degrees) to its radian equivalent.

Example 2:

(angtof “0.5p”)
; This will return 1.570796

The function converts the angle “0.5p” (half of pi radians) to its equivalent radian value.

Example 3:

Converting a string representing an angle in degrees, minutes, and seconds to its decimal degree representation.

(angtof “45d30’0” 1)
; This will return 45.5

In this example, we use the angtof function to convert the string “45d30’0” (which represents an angle of 45 degrees and 30 minutes) into its decimal degree equivalent. By specifying the units parameter as 1, we indicate that the input string is in the degrees-minutes-seconds format. The function returns the angle in decimal degrees, which in this case is 45.5 degrees.


angtos

Definition: Converts an angle value to a string.

Syntax:

(angtos angle [mode [precision]])

Example 1:

(angtos 0.785398 0 2)
; This will return “45.00”

This code converts the radian angle 0.785398 to a degree representation with two decimal places.

Example 2:

(angtos 1.570796 1)
; This will return “90d”

This function converts the radian angle 1.570796 to a degree format with the “d” suffix.


itoa

Definition: Converts an integer to a string.

Syntax:

(itoa integer)

Example 1:

(itoa 25)
; This will return “25”

This function simply converts the integer 25 into a string representation.

Example 2:

(itoa -100)
; This will return “-100”

It converts the negative integer -100 to its string equivalent.


atoi

Definition: Converts a string to an integer.

Syntax:

(atoi string)

Example 1:

(atoi “25”)
; This will return 25

This code transforms the string “25” into its integer form, 25.

Example 2:

(atoi “-100”)
; This will return -100

This function parses the string “-100” and converts it into the integer -100.


atof

Definition: Converts a string to a real number.

Syntax:

(atof string)

Example 1:

(atof “3.14”)
; This will return 3.14

This function translates the string “3.14” into the real number 3.14.

Example 2:

(atof “-0.007”)
; This will return -0.007

It processes the string “-0.007” and returns its real number counterpart, -0.007.

Example 3:

(atof “3”)
; This will return 3.0

 

To wrap things up, string conversions in AutoLISP are not just about moving data from one format to another. They are instrumental in bridging the gap between user input, computational processes, and output representation. Through consistent practice and application, you’ll find that these conversion functions become second nature, enhancing the efficiency and accuracy of your AutoLISP routines. Keep experimenting, keep questioning, and most importantly, keep coding!