怎么将一个数值按BCD码的形式直接存到一个数组中

如何将一个数值按BCD码的形式直接存到一个数组中
(1)如何将一个数值变量N的值按BCD码的形式直接存到一个数组中,比如 12345678 存到数组a(3)中,存完后的结果应为

a(0)低4位为:8 高4位为:7
a(1)低4位为:6 高4位为:5
a(2)低4位为:4 高4位为:3
a(3)低4位为:2 高4位为:1

(2) 如何将a(3) 中的数值再按高低顺序 存到数值变量N中。



------解决方案--------------------
个人思路:
1、将数据转换为BCD编码
2、将转换后的数据存入数组的相应的单元中
------解决方案--------------------
我的理解可由16进制数简化理解: 
每2个BCD码构成一字节,每字节二进制数据需处理成16进制的字符形式。仅是由00-99的范围。而非00-FF范围。
其中使用Mid函数分割12345678字符串为:12 34 56 78
然后用&H12 &H34 &H56 &H78 分别赋值给Byte数组.

LZ可参阅: http://iask.sina.com.cn/b/8576002.html
------解决方案--------------------
LZ可参阅:http://iask.sina.com.cn/b/8576002.html
------解决方案--------------------
VB code
Dim a(3) As Byte  '或者 Integer、Long

Private Sub Command1_Click()
'Long ---> BCD
   Dim lVal&
   Dim i&, t&
   lVal = CLng(Text1.Text)
'   Debug.Print lVal
' lVal 中保存待转换的值
' 自己添加异常处理的代码
   For i = 0 To 3
      t = lVal Mod 100
      lVal = lVal \ 100
      a(i) = t Mod 10 + (t \ 10) * 16
   Next
' 检查结果
'   For i = 0 To 3
'      Debug.Print Hex$(a(i))
'   Next
End Sub

Private Sub Command2_Click()
'BCD ---> Long
   Dim lVal&
   Dim i&, t&
' lVal 中保存转换结果
   lVal = 0
   For i = 3 To 0 Step -1
      t = a(i)
      lVal = lVal * 100 + (t And 240) * 5 \ 8 + (t And 15)
   Next
' 检查结果
'   Debug.Print ">>>>>> " & lVal
End Sub

------解决方案--------------------
实际测试,还是比较好用的……
VB code

Private Sub Form_Load()
  Dim tIndex As Long
  Dim tBytes() As Byte
  tBytes() = BCD_EnCode(12345678)
  For tIndex = 3 To 0 Step -1
    Text1.Text = Text1.Text & Hex(tBytes(tIndex))
  Next
  Text2.Text = BCD_DeCode(tBytes())
End Sub

Function BCD_EnCode(ByVal pValue As Long, Optional ByVal pOutLen As Long = 3) As Byte()
  Dim tOutBytes() As Byte
  Dim tBit(1) As Byte
  Dim tOutBytes_Index As Long
  ReDim tOutBytes(pOutLen)

  Do
    tBit(0) = (pValue Mod 10): pValue = pValue \ 10
    tBit(1) = (pValue Mod 10): pValue = pValue \ 10
    tOutBytes(tOutBytes_Index) = tBit(1) * &H10 + tBit(0)
    tOutBytes_Index = tOutBytes_Index + 1
  Loop While CBool(tBit(1)) And (tOutBytes_Index <= pOutLen)
  
  BCD_EnCode = tOutBytes()
End Function

Function BCD_DeCode(ByRef pBytes() As Byte) As Long
  Dim tOutValue As Long
  Dim tBit(1) As Byte
  Dim tOutBytes_Index As Long
  Dim tOutBytes_Length As Long
  
  tOutBytes_Length = UBound(pBytes())
  
  For tOutBytes_Index = tOutBytes_Length To 0 Step -1
    tBit(0) = pBytes(tOutBytes_Index) Mod &H10
    tBit(1) = pBytes(tOutBytes_Index) \ &H10
    tOutValue = tOutValue + (tBit(1) * 10 + tBit(0)) * 100 ^ tOutBytes_Index
  Next
  
  BCD_DeCode = tOutValue
End Function

------解决方案--------------------
VB code

Private Sub Command1_Click()
    Dim n, n1 As String, a(3), i As Integer
    n = 12345678
    n1 = Right("00000000" & CStr(n), 8)
    
    For i = 0 To Len(n1) / 2 - 1
        a(i) = a(i) & Mid(n1, Len(n1) - 2 * i, 1) & Mid(n1, Len(n1) - (2 * i + 1), 1)
    Next
    
    Debug.Print Join(a, "")
    
    n1 = ""
    For i = 3 To 0 Step -1
        n1 = n1 & Right(a(i), 1) & Left(a(i), 1)
    Next
    n = Val(n1)
   
    Debug.Print n

       
End Sub