主题:win7系统下半角字符与全角字符互相转换的代码
win7系统下半角字符与全角字符互相转换的代码
在XP系统中,半角字符与全角字符互相转换可以用VB函数StrConv,很方便,但在win7系统中就会出错。这就需要自己来编写转换函数,见以下代码。由于代码简单清晰,就不作分析了,只注意这一句:
f(94) = Chr(-24149)
因为在系统的汉字表中,f(94)所对应的Ascii码为-23554的汉字符并不是“~”,所以需要我们修改一下,以便和h(94)的“~”对应。
本代码在win7旗舰版通过,未在XP机上试验。
Private Sub Form_Load()
Text1 = "123 英文字母 abc ABC !@#$%^&*()_+/\~"
End Sub
Private Sub Command1_Click() '半角转全角
Text1 = half2full(1, Text1)
End Sub
Private Sub Command2_Click() '全角转半角
Text1 = half2full(2, Text1)
End Sub
Private Function half2full(Index As Integer, st As String) As String
Dim halfSt As String '半角字符串
Dim fullSt As String '全角字符串
Dim h(94) As String '半角字符
Dim f(94) As String '全角字符
Dim i As Integer, j As Long, z As String
For i = 0 To 94: h(i) = Chr(i + 32): f(i) = Chr(i - 23648): Next
f(94) = Chr(-24149)
halfSt = Join(h)
fullSt = Join(f)
For j = 1 To Len(st)
z = Mid(st, j, 1)
Select Case Index
Case 1: i = InStr(halfSt, z): If i Then st = Replace(st, z, Mid(fullSt, i, 1))
Case 2: i = InStr(fullSt, z): If i Then st = Replace(st, z, Mid(halfSt, i, 1))
End Select
Next
half2full = st
End Function
在XP系统中,半角字符与全角字符互相转换可以用VB函数StrConv,很方便,但在win7系统中就会出错。这就需要自己来编写转换函数,见以下代码。由于代码简单清晰,就不作分析了,只注意这一句:
f(94) = Chr(-24149)
因为在系统的汉字表中,f(94)所对应的Ascii码为-23554的汉字符并不是“~”,所以需要我们修改一下,以便和h(94)的“~”对应。
本代码在win7旗舰版通过,未在XP机上试验。
Private Sub Form_Load()
Text1 = "123 英文字母 abc ABC !@#$%^&*()_+/\~"
End Sub
Private Sub Command1_Click() '半角转全角
Text1 = half2full(1, Text1)
End Sub
Private Sub Command2_Click() '全角转半角
Text1 = half2full(2, Text1)
End Sub
Private Function half2full(Index As Integer, st As String) As String
Dim halfSt As String '半角字符串
Dim fullSt As String '全角字符串
Dim h(94) As String '半角字符
Dim f(94) As String '全角字符
Dim i As Integer, j As Long, z As String
For i = 0 To 94: h(i) = Chr(i + 32): f(i) = Chr(i - 23648): Next
f(94) = Chr(-24149)
halfSt = Join(h)
fullSt = Join(f)
For j = 1 To Len(st)
z = Mid(st, j, 1)
Select Case Index
Case 1: i = InStr(halfSt, z): If i Then st = Replace(st, z, Mid(fullSt, i, 1))
Case 2: i = InStr(fullSt, z): If i Then st = Replace(st, z, Mid(halfSt, i, 1))
End Select
Next
half2full = st
End Function