VBScript Function to Convert an Integer to Binary String Represe
- Time:2020-09-17 10:57:36
- Class:Weblog
- Read:29
Given a Integer, we want to convert it to binary string. We can do this iteratively, by concatenating the modulus by two result and shifting the number 1 position to the right. We also need to check if it is negative number and appending a ‘-‘ sign before the result accordingly.
Implementing the ToBinaryString in VBScript is as follows. The zero is a special case. And to shift an integer one position to the right. We can use the Int(Num/2). The CInt() function will round up the values for example, CInt(3.5) is 4. Thus the Int() function in VBScript is equivalent to the Floor Function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Function ToBinaryString(ByVal Num) Dim s, sign: sign = "" If Num < 0 Then sign = "-" Num = -Num ElseIf Num = 0 Then ToBinaryString = "0" Exit Function End If While Num > 0 s = Chr(48 + (Num Mod 2)) & s Num = Int(Num / 2) Wend ToBinaryString = sign & s End Function |
Function ToBinaryString(ByVal Num) Dim s, sign: sign = "" If Num < 0 Then sign = "-" Num = -Num ElseIf Num = 0 Then ToBinaryString = "0" Exit Function End If While Num > 0 s = Chr(48 + (Num Mod 2)) & s Num = Int(Num / 2) Wend ToBinaryString = sign & s End Function
The results are shown in the VBSEdit IDE – and you can verify a few integers from negative 15 to positive 15, respectively.

vbscript-convert-integer-to-binary-in-vbsedit
–EOF (The Ultimate Computing & Technology Blog) —
Recommend:Harddrives will fail – it is just a matter of when
Bruteforce/DFS/Backtracking Algorithm using Javascript to Solve
DFS and BFS Algorithm to Find Numbers With Same Consecutive Diff
How to Remove Items/Entries with Specific Values from Map/HashMa
Find the Real Root of 4^x + 6^x = 9^x
Depth First Search (Backtracking) Algorithm to Solve a Sudoku Ga
Using Bitmasking Algorithm to Compute the Combinations of an Arr
Flashing the BIOS of HPZ800 Server to 3.61 Rev.A
Algorithm to Sum The Fibonacci Numbers
How to Adapt Your Blog to Increasing Zero-Click Searches
- Comment list
-
- Comment add