VBScript中是否内置了一个函数(wscript
或cscript
),它会取一个数字并将其转换为基数2?
例如,Base2(45)
会输出"101101"
。
答案 0 :(得分:3)
我不知道内置任何内容,但是创建一个可以处理二进制和其他基础的通用例程很容易。如果您将符号从0
定义为Z
,则可以处理基本为36的所有内容。
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
作为基础:
WScript.Echo ToBase(45, 2)
输出:
101101
答案 1 :(得分:1)
在二进制文件中编码负数,如程序员模式中的calc
,即仅整数模式(但{{1}仅减少精确到32位):
VBScript
<强>输出强>:
option explicit
On Error GoTo 0
Dim xx
xx = 45
Wscript.Echo +xx, vbTab, Base2( xx, False), Base2( xx, True)
Wscript.Echo -xx, vbTab, Base2(-xx, False), Base2(-xx, True)
Function Base2( iNum, bLong)
Dim ii, octets, sNum, iLen
octets = Array ( "000","001", "010", "011", "100", "101", "110", "111")
If bLong Or Len( CStr( Hex( -Abs(iNum)))) > 4 Then
sNum = CStr( Oct(CLng(iNum))) 'force Long : DWORD (32 bits/4 bytes)
iLen = 32
Else
sNum = CStr( Oct(CInt(iNum))) 'keep Integer: WORD (16 bits/2 bytes)
iLen = 16
End If
Base2 = ""
For ii = 1 To Len( sNum)
Base2 = Base2 & octets( Mid( sNum, ii, 1))
Next
Do While Len( Base2) > 1 And Left( Base2, 1) = "0"
Base2 = Mid( Base2, 2) 'truncate left zeroes
Loop
'expand left zeroes for a positive value?
'Base2 = Right( String( iLen, "0") & Base2, iLen)
End Function
输出,==>cscript //NOLOGO D:\VB_scripts\SO\32416311.vbs
45 101101 101101
-45 1111111111010011 11111111111111111111111111010011
==>
取消注释:
Base2 = Right( String( iLen, "0") & Base2, iLen)
答案 2 :(得分:0)
从c库代码中窃取的更通用(更安全)的方法:
<?php
if(isset($_POST['id']) && is_numeric($_POST['id']))
{
// run query to get first_name where the submitted record id matches
$toCD = "SELECT `first_name` FROM `users` WHERE `id` = intval($id)";
$toST = $con->query($toCD);
// make sure a row was return
if($toST->num_rows === 1)
{
$row = $toST->fetch_assoc();
echo "<div class = 'prep_stmt'>Your message to".$_row["first_name"]."...</div>";
}
else
{
// record id does not exist. Output error message
}
}
?>
输出:
Option Explicit
Function ntoa( nNum, iBase )
ntoa = "0"
If nNum Then
ntoa = Mid( "-", Sgn( nNum ) + 2 ) + ntoaPos( Abs( nNum ), iBase )
End If
End Function
Function ntoaPos( nNum, iBase )
If nNum >= 1 Then
Dim nD : nD = Fix( nNum / iBase )
Dim nM : nM = nNum - nD * iBase
ntoaPos = ntoaPos( nD, iBase ) _
& Mid( "0123456789ABCDEFGHIJKLMNOPQRSTUV", 1 + nM, 1 )
End If
End Function
Function aton( ByVal sNum, iBase )
sNum = Trim( sNum )
Dim nLen : nLen = Len( sNum )
Dim bNeg, nPos
Select Case Left( sNum, 1 )
Case "+"
bNeg = False
nPos = 2
Case "-"
bNeg = True
nPos = 2
Case Else
bNeg = False
nPos = 1
End Select
aton = "0"
For nPos = nPos To nLen
Dim nAdd : nAdd = Instr( "0123456789ABCDEFGHIJKLMNOPQRSTUV", Mid( sNum, nPos, 1 ) )
If 0 = nAdd Then
' Error
End If
aton = aton * iBase + nAdd - 1
Next
If bNeg Then
aton = - aton
End If
End Function
' use ByVal or don't change the parameter!
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
Dim xTests
Dim oWAU : Set oWAU = WScript.Arguments.Unnamed
If 0 = oWAU.Count Then
Set xTests = CreateObject("System.Collections.ArrayList")
xTests.Add 45
xTests.Add 2
Else
Set xTests = WScript.Arguments.Unnamed
End If
Dim i, n, b, s, o, r
For i = 1 To xTests.Count Step 2
n = Eval(xTests(i - 1))
b = xTests(i)
s = ntoa(n, b)
On Error Resume Next
o = ToBase(n, b)
If Err.Number Then
o = Err.Description
End If
On Error GoTo 0
r = aton(s, b)
WScript.Echo n, b, "==>", s, "<==", r, "?", CStr(n = r), o
Next