深圳前海售电公司好吗:vb的问题:转换字符为数字

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/29 08:11:48
想自定义一个这样的规则:即键盘的字母键(不分大小写)均自动对应一个数字,如:
A→01;B→02;C→03;……;Z→26
定义好以后,要实现这样的转换:可以对随机的一个字母单词自动转换为数字表示。如ASD(或Asd,ASd,AsD等不分大小写的)转换成“011904”。
求助高手,用代码怎么实现?

首先定义一个函数:
Function trans(ByVal S As String) As String
Dim i As Integer
Dim t As String
S = UCase(S)
For i = 1 To Len(S)
t = Asc(Mid(S, i, 1)) - 64
If t < 10 Then
trans = trans & "0" & t
Else
trans = trans & t
End If
Next
End Function

在程序中调用此函数即可,如:
ss=trans("ASD")

asc('A')-A的ascii码+1

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">

<script language="vbscript">
Function MyCode(ByVal s)
s=LCase(s)
For i=1 To Len(s)
MyCode=MyCode & Decode(Mid(s,i,1))
Next
End Function

Function Decode(ByVal str)
Decode=Asc(str) - 32
End Function

document.write(MyCode("abcdefg,ABCDEFG"))
//-->
</SCRIPT>

数组啊,
dim a(1 to 26)
a(1)="01"
a(2)="02"
……
function fac(byval a as string) as string
dim i
dim s as string
a=lcase(a)
for i=1 to len(a)
s=s & a(asc(mid(a,i,1))-64)
next
fac=s
end function

然后fac调用