本文介绍了VBScript中的基本转换功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VBScript中是否内置了一个函数(用于wscriptcscript),该函数会接受一个数字并将其转换为以2为底的数字?

Is there a function built into VBScript (for wscript or cscript) that would take a number and convert it to base 2?

例如,Base2(45)将输出"101101".

推荐答案

我不知道任何内置的东西,但是创建可以处理二进制和其他基数的通用例程非常容易.如果定义从0Z的符号,则可以处理以36为底的所有内容.

I'm not aware of anything built-in, but it's easy enough to create a general-purpose routine that can handle binary and other bases. If you define symbols from 0 to Z, you can handle everything up to base 36, for example.

Function ToBase(ByVal n, b)

    ' Handle everything from binary to base 36...
    If b < 2 Or b > 36 Then Exit Function

    Const SYMBOLS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    Do
        ToBase = Mid(SYMBOLS, n Mod b + 1, 1) & ToBase
        n = Int(n / b)
    Loop While n > 0

End Function

以您的示例为例,只需将2用作基础:

For your example, just pass 2 for the base:

WScript.Echo ToBase(45, 2)

输出:

101101

这篇关于VBScript中的基本转换功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:39