Data Services Functions, an exercise in base number conversions Data Services (DS) scripting language is often seen purely as a means for setting global variables or
handling calls to OS level commands. While it is not designed for complex coding or cursor operations
the basic operations available in DS lend themselves to a variety of applications. You will see some of
those capabilities demonstrated in this article implementing numeric case conversions from and to
Binary, Octal, Decimal and Hexadecimal.
Converting Hexadecimal numbers to Binary numbers
Let’s start out with a function to convert hexadecimal (base 16) numbers over to binary (base 2). Figure
1 contains the header comment block, a break down by section follows.
Figure 1 – Header Comment Block
The first feature you should utilize when using the DS scripting language is the comment. Comments
start with a pound (#) sign and everything after that symbol is ignored during script execution.
Comments are shown in the auto-documentation generated by DS and can serve as a version log,
function parameter summary and notepad for why you used a particular method instead of other
alternatives in the code.
The header comment block should include the following sections (numbered in Figure 1 for
identification purposes):
Section 1 contains a simple function usage schematic. The schematic allows you to see at a glance the data type and length of each parameter as well te parameters usage in the function (input, output or inout).
Section 2 provides basic information about who wrote the function, its intended use and when it was authored.
Section 3 gives a chronological log of changes to the function and a basic description of the change made.
Section 4 lists the locally scoped variables used with type, length and a short description.
Section 5 provides an area for notes that can be used for any general information about the function, limitations, or other pertinent facts for later reference.
Figure 2 - Variable Setting
At this point in the script, you should set your variables to their starting values. While this can be done
later in the script, grouping and setting them all in the same place make locating them simple as you
follow the code or begin troubleshooting. Figure 2 shows an example of variable values that are utilized
in this script. Use the equal operator for assignment and the semi-colon to end the statement.
Figure 3 - While loop
In Figure 3 you see a while statement, which is the looping structure used in DS scripting. If you are
familiar with the While transform or almost any iterative control statement (FOR-NEXT, etc) this
structure will be easy to follow. It starts with a condition to be satisfied and continues repeating the
steps defined until the condition fails to be met. In cases where multiple satisfying conditions are
possible you should take care to order them in such a way that the desired condition-value pair is
evaluated first. If no condition is satisfied then the final value of the decode statement (an ELSE value) is
returned.
In this example, the first hex character is selected and the binary equivalent is assigned to the output
string. The assignment is done by appending (using a double pipe concatenation operator) the result of
a decode statement to the end of output variable that will be passed back from the function. The
decode statement works by evaluating conditions and returning a value (a condition-value pair). When a
condition is satisfied the value portion of the condition-value pair is returned and no further conditions
are evaluated. Then the loop counter increments and the code repeats until there are no more
characters to convert.
Figure 4 - Return result
Lastly Figure 4 shows the output string returned. It contains the binary number that was built by the
while loop in Figure 3.
Converting Binary to Octal
The reverse of converting binary to hexadecimal would be very similar to what I described so far, so let’s
take a look at a binary to octal conversion to review a few other DS script commands.
Figure 5 - Binary to octal
Figure 5 shows the basic structure and variable defaults, which are very similar to the example covered
in Figure 1. You can read through it again and see what is going on, if needed.
Figure 6 - Positional padding
In Figure 6, you see the mod function used to append additional placeholder values at the beginning of
the binary input string to make number of positions evenly divisible by 3. This makes conversion easier
by providing consistent binary triplets for assignment to octal.
Figure 7 - Conversion code Figure 8 - Conversion code, revised
The difference you may notice from Figure 3 to Figure 7 is the addition of a substring to remove leading
zeros. This is done because binary numbers often are a set length with leading zeros and octal is not
commonly displayed with a leading zero. The substring was used in Figure 7 to show that there are often
multiple ways to accomplish a desired outcome in DS script. In this case, it is simpler to use another
native DS function, LTRIM. See Figure 8 for revised code using the LTRIM function.
Figure 9 – Conditional calls
When you have a need to do accomplish a task on a variety of different input types in the context of a
function, you can accomplish it with a structure similar to the one in Figure 9. This nesting of functions
can make your code more versatile and reusable at the same time. In this snippet, inside a wrapper
function called fn_base_convert, other functions are called as needed to return the desired value. You
can mix calls to other functions or use additional code to handle scenarios where no existing function
fits your needs. The decimal conversion uses another DS script function called POWER to calculate the
decimal value of a binary position. It calculates the decimal value of a given position by multiplying the
binary value by two to the power of that position minus one, represented as 2(length-iteration).
In conclusion, Data Services scripting language can accomplish a variety of tasks that you might
otherwise have to rely on database functions or other outside tools to accomplish. This allows you to
maintain all your code in a single location reducing the need for multiple toolsets and speeds
development through re-use. If you would like to play with the functions and come up with more
efficient ways to do the conversions or show off a different set of commands, feel free to download the
scripts in the linked file.
Ernie Phelps, Business Intelligence Consultant
Decision First Technologies
Ernie Phelps is a BI consultant with 15 years experience in Enterprise Information Management (EIM).
He is the Chair of ASUG’s Data Management Special Interest Group and previous Customer Chair of the
EIM Influence Council. At Decision First Technologies, Ernie fulfills a variety of roles including data
modeling, ETL development, mentoring, profiling and teaching.