标题: 【求助】BCD码转换为ASCii码 [打印本页] 作者: ~血色一枝梅~ 时间: 2009-2-28 08:24 PM 标题: 【求助】BCD码转换为ASCii码 各位帅气英俊,能力过人的大大们
请问一下,要实现这个功能,该怎么办?
我在MSDN社区那里找到了以下的:
'由ASCII码转BCD码
Function AscToBCD(ASCII() As Byte) As Byte()
Dim i As Integer
Dim bTemp As Byte
Dim bBCD() As Byte
Dim bAsc() As Byte
Dim bA As Byte
Dim bB As Byte
Dim intLen As Integer
intLen = UBound(ASCII)
If intLen Mod 2 = 0 Then intLen = intLen + 1
ReDim bAsc(intLen) As Byte
ReDim bBCD((intLen + 1) / 2 - 1) As Byte
For i = 0 To UBound(ASCII)
bAsc(i) = ASCII(i)
Next
If intLen > i Then bAsc(intLen) = &H0 '对数组不是偶数的补位
For i = 0 To intLen
If bAsc(i) < Asc("0") Then
bAsc(i) = Asc("0")
ElseIf ((bAsc(i) > Asc("9")) And (bAsc(i) < Asc("A"))) Then
bAsc(i) = Asc("0")
ElseIf ((bAsc(i) > Asc("F")) And (bAsc(i) < Asc("a"))) Then
bAsc(i) = Asc("0")
ElseIf (bAsc(i) > Asc("f")) Then
bAsc(i) = Asc("0")
End If
If (bAsc(i) >= Asc("0") And bAsc(i) <= Asc("9")) Then
bA = bAsc(i) - Asc("0")
ElseIf (bAsc(i) >= Asc("a") And bAsc(i) <= Asc("z")) Then
bA = bAsc(i) - Asc("a") + &HA
Else
bA = bAsc(i) - Asc("A") + &HA
End If
i = i + 1
If bAsc(i) < Asc("0") Then
bAsc(i) = Asc("0")
ElseIf ((bAsc(i) > Asc("9")) And (bAsc(i) < Asc("A"))) Then
bAsc(i) = Asc("0")
ElseIf ((bAsc(i) > Asc("F")) And (bAsc(i) < Asc("a"))) Then
bAsc(i) = Asc("0")
ElseIf (bAsc(i) > Asc("f")) Then
bAsc(i) = Asc("0")
End If
If (bAsc(i) >= Asc("0") And bAsc(i) <= Asc("9")) Then
bB = bAsc(i) - Asc("0")
ElseIf (bAsc(i) >= Asc("a") And bAsc(i) <= Asc("z")) Then
bB = bAsc(i) - Asc("a") + &HA
Else
bB = bAsc(i) - Asc("A") + &HA
End If
bBCD((i - 1) / 2) = (bA * 16) Xor bB
Next
AscToBCD = bBCD
End Function
---------------------------------------------------------------
调用BCDToAsc
Private Sub Command1_Click()
Dim bA(3) As Byte
Dim bB() As Byte
Dim i As Integer
bB = AscToBCD(bA)
For i = 0 To UBound(bB)
MsgBox Hex(bB(i))
Next
End Sub
结果为 &H10 和&HAF
---------------------------------------------------------------
'由BCD转ASCII码
Function BCDToAsc(BCD() As Byte) As Byte()
Dim i As Integer
Dim bTemp As Byte
Dim bAsc() As Byte
Dim intLen As Integer