声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 3249|回复: 3

[VB] 求助!vb编程--BCD码转换成文本

[复制链接]
发表于 2006-5-8 21:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
怎样用vb6.0编数字转换系统?计算机接受的BCD码,要转换成文本文件.快帮帮我啊!拜托拉!

[ 本帖最后由 风花雪月 于 2006-11-20 08:52 编辑 ]
回复
分享到:

使用道具 举报

发表于 2006-5-14 20:03 | 显示全部楼层

求助!vb编程!

用BYTE接收双BCD码,然后通过运算(对十六取模)分离两个 BCD码
[此贴子已经被作者于2006-5-14 20:04:34编辑过]

发表于 2006-9-1 07:20 | 显示全部楼层
budong
发表于 2006-9-2 06:49 | 显示全部楼层
'由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  
         
       intLen  =  UBound(BCD)  
       ReDim  bAsc(intLen  *  2  +  1)  As  Byte  '重新定义数组上标  
         
       For  i  =  0  To  intLen  
               bTemp  =  (BCD(i)  /  16)  And  &HF  
               If  bTemp  >  9  Then  
                       bAsc(i  *  2)  =  bTemp  +  Asc("A")  -  10  
               Else  
                       bAsc(i  *  2)  =  bTemp  +  Asc("0")  
               End  If  
                 
               bTemp  =  BCD(i)  And  &HF  
                 
               If  bTemp  >  9  Then  
                       bAsc(i  *  2  +  1)  =  bTemp  +  Asc("A")  -  10  
               Else  
                       bAsc(i  *  2  +  1)  =  bTemp  +  Asc("0")  
               End  If  
       Next  
         
       BCDToAsc  =  bAsc  
End  Function  

---------------------------------------------------------------

'由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  
         
       bA(0)  =  Asc("1")  
       bA(1)  =  Asc("0")  
       bA(2)  =  Asc("a")  
       bA(3)  =  Asc("F")  
         
       bB  =  AscToBCD(bA)  
       For  i  =  0  To  UBound(bB)  
               MsgBox  Hex(bB(i))  
       Next  
End  Sub
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-18 05:06 , Processed in 0.066102 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表