帮忙翻译ASP脚本 ( 积分: 100 )

  • 主题发起人 主题发起人 7030
  • 开始时间 开始时间
7

7030

Unregistered / Unconfirmed
GUEST, unregistred user!
脚本如下,是用来随机生成中文姓名的,自己对ASP不懂,只能寻求帮助了
<%
Dim i
Dim j
Dim HS
Dim HE
Dim LS
Dim LE
dim Result
Result=&quot;&quot;
HS = 177
HE = 247
LS = 161
LE = 254
dim Max_Num
Max_num=65536
Randomize
For i = 1 To 4
temp1=dec2bin(Int((HE - HS) * Rnd()) + HS)
temp2=dec2bin(Int((LE - LS) * Rnd()) + LS)
Result = Result &amp
chr(BinaryToDecimal(temp1 &amp
temp2) - Max_num)
Next
response.write Result &amp
&quot
&quot;
%>
<%
'下面是函数
Public Function dec2bin(mynum)'十进制到二进制
Dim loopcounter
If mynum >= 2 ^ 31 Then
dec2bin = &quot;Too big&quot;
Exit Function
End If
Do
If (mynum And 2 ^ loopcounter) = 2 ^ loopcounter Then
dec2bin = &quot;1&quot
&amp
dec2bin
Else
dec2bin = &quot;0&quot
&amp
dec2bin
End If
loopcounter = loopcounter + 1
Loop Until 2 ^ loopcounter > mynum
End Function

Public Function BinaryToDecimal(BinaryValue) '二进制到十进制的转换
' Returns the decimal equivalent of a binary number
Dim idx
Dim tmp
Dim result
Dim digits
digits = Len(BinaryValue)
For idx = digits To 1 Step -1
tmp = Mid(BinaryValue, idx, 1)
If tmp = &quot;1&quot
Then result = result + 2 ^ (digits - idx)
Next
BinaryToDecimal = result
End Function
%>
 
function RandomCreateChineseName:WideString;
var
i:Integer;
HighByte,LowByte:WORD;
const
HS:Byte = 177;
HE:Byte = 247;
LS:Byte = 161;
LE:Byte = 254;
Max_num=65536;
begin
Result:='';
Randomize

for i := 1 To 4 do
begin
HighByte:=Trunc((HE - HS) * Random) + HS;
HighByte:=HighByte Shl 8;
LowByte:=Trunc((LE - LS) * Random) + LS;
Result:=Result+WideChar(HighByte+LowByte-Max_num);
end;
end;
 
逻辑是如此,但是得到结果都是问号。不知道何故?
 
看看你的编码格式,和asp文件的编码格式都用gb2312或者都用UTF-8
 
偶对asp一窃不通,对汉字编码也不懂,用LoveShanShan的函数结果全是问号,不知为什么
 
可能是UTF-8的,ansi文字编码范围为$8140~$A0FE,同时低位$0~$39保留
楼上的汉字取值范围略微改下就能用了
function RandomCreateChineseName:String;
var
i:Integer;
HighByte,LowByte:WORD;
const
HS:Byte = $81;
HE:Byte = $A0;
LS:Byte = $40;
LE:Byte = $FE;
begin
Result:='';
Randomize;
for i := 1 To 4 do
begin
HighByte:=Random(HE - HS) + HS;
HighByte:=HighByte Shl 8;
LowByte:=Random(LE - LS) + LS;
Result:=Result+WideChar(HighByte+LowByte);
end;
end;
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
687
import
I
I
回复
0
查看
651
import
I
I
回复
0
查看
630
import
I
后退
顶部