BCD码文件转换为ASCII码问题

szyss 2004-04-15 12:06:15
大家好!请问怎么将BCD码文件转换为ASCII码。
多谢!
...全文
103 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
KiteGirl 2004-04-16
  • 打赏
  • 举报
回复
以下是速度测试代码:

Private Sub Command2_Click()
Dim tBytes() As Byte
Dim tIndex As Long
Dim tDemoCount As Long
Dim tString As String
Dim tOnTimer As Double
Dim tTimerOver As Double
tDemoCount = 1000000
ReDim tBytes(tDemoCount) As Byte
For tIndex = 0 To tDemoCount
tBytes(tIndex) = tIndex Mod 10
Next
tOnTimer = Timer
tString = StringGetByBCDs(tBytes())
tTimerOver = Abs(Timer - tOnTimer)
Text1.Text = tTimerOver & " " & Len(tString)
End Sub

一百万字节数据测试。在我家最慢的一台机器运行(赛扬433):解释运行3.4秒;编译后0.6秒。
KiteGirl 2004-04-16
  • 打赏
  • 举报
回复
测试代码:
Private Sub Form_Load()
Dim tBytes() As Byte
ReDim tBytes(255) As Byte

'产生一个0到255值的Byte数组。
For I = 0 To 255
tBytes(I) = I
Next

Text1.Text = StringGetByBCDs(tBytes())
End Sub

Function StringGetByBCDs(ByRef pBCDs() As Byte) As String
'从一个BCD码Byte数组获得一个UniCode字符串。不合法的BCD码以"X"代替。
Dim tOutStr As String
Dim tBytes() As Byte

tBytes() = AsciisGetByBCDs(pBCDs())
tOutStr = StrConv(tBytes(), vbUnicode)

StringGetByBCDs = tOutStr
End Function

Function AsciisGetByBCDs(ByRef pBCDs() As Byte) As Byte()
'从一个BCD码Byte数组获得一个Ascii码Byte数组。不合法的BCD码以"X"代替。
Dim tOutBytes() As Byte
Dim tIndex As Long
Dim tBCDs_Index As Long
Dim tBCDs_Index_Start As Long
Dim tBCDs_Index_End As Long
Dim tBCDs_Length As Long

Dim tBCD_ValueH As Long
Dim tBCD_ValueL As Long

Dim tAsciis_Index As Long
Dim tAsciis_Length As Long

tBCDs_Index_Start = LBound(pBCDs)
tBCDs_Index_End = UBound(pBCDs)
tBCDs_Length = tBCDs_Index_End - tBCDs_Index_Start

tAsciis_Length = (tBCDs_Length + 1) * 2 - 1

ReDim tOutBytes(tAsciis_Length)

For tIndex = 0 To tBCDs_Length
tBCDs_Index = tIndex + tBCDs_Index_Start

tBCD_ValueH = pBCDs(tBCDs_Index) \ 16
tBCD_ValueL = pBCDs(tBCDs_Index) Mod 16

tBCD_ValueH = ((tBCD_ValueH < 10) And tBCD_ValueH) + ((tBCD_ValueH > 9) And 40)
tBCD_ValueL = ((tBCD_ValueL < 10) And tBCD_ValueL) + ((tBCD_ValueL > 9) And 40)

tAsciis_Index = tIndex * 2

tOutBytes(tAsciis_Index) = tBCD_ValueH + 48
tOutBytes(tAsciis_Index + 1) = tBCD_ValueL + 48
Next

AsciisGetByBCDs = tOutBytes()
End Function

以下是测试结果:

000102030405060708090X0X0X0X0X0X
101112131415161718191X1X1X1X1X1X
202122232425262728292X2X2X2X2X2X
303132333435363738393X3X3X3X3X3X
404142434445464748494X4X4X4X4X4X
505152535455565758595X5X5X5X5X5X
606162636465666768696X6X6X6X6X6X
707172737475767778797X7X7X7X7X7X
808182838485868788898X8X8X8X8X8X
909192939495969798999X9X9X9X9X9X
X0X1X2X3X4X5X6X7X8X9XXXXXXXXXXXX
X0X1X2X3X4X5X6X7X8X9XXXXXXXXXXXX
X0X1X2X3X4X5X6X7X8X9XXXXXXXXXXXX
X0X1X2X3X4X5X6X7X8X9XXXXXXXXXXXX
X0X1X2X3X4X5X6X7X8X9XXXXXXXXXXXX
X0X1X2X3X4X5X6X7X8X9XXXXXXXXXXXX
KiteGirl 2004-04-16
  • 打赏
  • 举报
回复
0x02对应"02"
0x10对应"10"

你要将字节0x02转换成字节0x30和字节0x32对吗?别着急!代码马上就来!
陈年椰子 2004-04-15
  • 打赏
  • 举报
回复
继续关注
tangxiaosan001 2004-04-15
  • 打赏
  • 举报
回复
str = Asc(StrConv(xxx, vbUnicode))
szyss 2004-04-15
  • 打赏
  • 举报
回复
xinliangyu的方法不行啊
xinliangyu 2004-04-15
  • 打赏
  • 举报
回复
第一步:转为unicode strConv(XXX,vbUnicode)
第二步:将Unicode转为Ascii码 asc("X")

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧