vbScript - Convert Integer to Binary String
Please see my post vbScript - The Basics for more details on vbScript.
vbScript has a number of functions for converting from one type to another. These functions are named C<type> as in CInt (convert to int), CDbl (convert to double), etc. There are also functions that will format numbers as string (FormatNumber, FormatPercent, FormatCurrentcy, Hex, etc.) One glaring omission is a function to format numbers (integers) as binary. Fortunately the process is straightforward.
If we first convert the number to e hexadecimal string we can then convert each hex digit into its equivalent four digit binary string. By finding the index (one relative) of the hex digit in the string 0123456789ABCDEF
and subtracting one, we get a zero relative number which we can then use as an index into an array that maps each hex digit to its four digit binary equivalent.
Since long binary strings are difficult to interpret, let's also provide a grouping function that will return the binary string in user specified groups. For example, Group(str,4) will return the string in groups of four characters separated by a space. The two functions are given here along with a short piece of test code. Note that Group
will work on any string, not just binary strings.