VBScript Function to Convert an Integer to Binary String Represe
- 时间:2020-09-17 10:57:36
- 分类:网络文摘
- 阅读:113 次
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) —
推荐阅读:给哪些网站暂时赚不到钱的站长鼓鼓劲 个人站长 建设网站贵在坚持 网站站长赚钱的6大好用的途径 整理6款站长赚钱方法 希望对你有所帮助 个人站长们常见的很多个网站盈利模式总结 春季饮食宜润肺,常吃炖梨既滋润又养人,口感甜香味道美 这道小学应用题比较难,解题关键是求相遇时间 豆腐搭配鸡蛋做出香酥可口的丸子,营养也很丰富 这道小学奥数题难倒多数学生,解题关键是比例 分享茄子的家常做法,吃起来不油腻,营养美味又下饭
- 评论列表
-
- 添加评论