[Dev] 一个简单的 Dump 转文本工具 --- Dump2Text

每次电脑重装都得烦心,要把庞大的 IDE 重新配置一次,正准备安装 Visual Studio 2010,上网找镜像的时候发现,Visual Studio 2013 推出了 Community 版,不仅没有 lite 掉,而且还全免费!这里 Post 个官方的镜像下载地址先 (Visual Studio Community 2013),破解版就别用啦,反正不是商用,社区版足矣。

既然安装好了最新版的 Visual Studio,自然要来弄点东西解解馋,最近一直在做 RFID 的项目,正需要一个 Windows 版的 dump 转 txt 工具,虽说有 Mifare Classic Tools 提供的 Bash 脚本,还有 Python 版,但 Windows 上使用始终是不方便,所以决定用 VB.Net 来做一个。不得不说,最新的 .Net 的确让人有点受不了,代码臃肿,但的确语言是越来越高级了,跟 F# 一样,开始接近人类自然语言了,效率嘛相对来说比之前也好了一些,但是毕竟是要用虚拟机运行的,要求就不要太高了。

不说废话,贴出关键代码。

  
    'Project Name:Dump2Text
    'Version:1.1
    'IDE:Visual Stdio 2013-Visual Basic.Net
    'Designed by Sirius Web-bobylive
    'http://bobylive.com
    'Email:i#boby.im (Replace # into @)
    'Date:2015-02-16
        Dim DumpData As String, ReadData As Byte, ReadCircle As Byte, ReadChangeLine As Byte, TextName As String, TextPath As String, FilePath As String = TextBoxFilePath.Text     '定义变量
        If FilePath <> "" Then        '保证文件目录不为空,否则下面处理将会出错,其实这里应该还增加一个检测文件是否存在的,1.2 版本的时候再奉上
            TextBoxFilePath.Text = FilePath
            Dim SetDump As New IO.FileStream(FilePath, IO.FileMode.Open)        '打开文件句柄,使用FileStream
            Dim ReaDump As New IO.BinaryReader(SetDump)     '因为是 binary 文件,所以使用 BinaryReader,byte读出
            TextName = My.Computer.FileSystem.GetName(FilePath)     '获取 Dump 的文件名
            TextPath = My.Computer.FileSystem.GetParentPath(FilePath)       '获取文件路径
            ReaDump.BaseStream.Position = 0     '指针设置为 0,VB 引入了指针还是很方便的
            DumpData = ""
            Select Case ReaDump.BaseStream.Length       '针对 1 kb 和 4 kb 的 Dump 进行处理
                Case 1024
                    Do
                        ReadData = ReaDump.ReadByte
                        ReadCircle += 1
                        DumpData &= Hex(ReadData).PadLeft(2, "0")       '修正读出字节格式
                        If ReadCircle = 16 Then     '分段
                            DumpData &= vbCrLf
                            ReadCircle = 0
                            ReadChangeLine += 1
                        End If
                        If ReadChangeLine = 4 Then      '分扇区
                            DumpData &= vbCrLf
                            ReadChangeLine = 0
                        End If
                    Loop While ReaDump.BaseStream.Position < ReaDump.BaseStream.Length       '到文件末端的时候结束读取,这种方法一大亮点
                    SetDump.Close()     '记得关闭文件句柄
                    ReaDump.Close()
                    My.Computer.FileSystem.WriteAllText(TextPath & "\" & TextName & ".txt", DumpData, False, System.Text.Encoding.Default)      '写入 txt
                    MessageBox.Show("Cotvert Successful!", "Congratulation!", MessageBoxButtons.OK)
                Case 4096
                    Do While ReaDump.BaseStream.Position < 1024      '忽略 4 kb 文件后面空白部分
                        ReadData = ReaDump.ReadByte
                        ReadCircle += 1
                        DumpData &= Hex(ReadData).PadLeft(2, "0")
                        If ReadCircle = 16 Then
                            DumpData &= vbCrLf
                            ReadCircle = 0
                            ReadChangeLine += 1
                        End If
                        If ReadChangeLine = 4 Then
                            DumpData &= vbCrLf
                            ReadChangeLine = 0
                        End If
                    Loop
                    SetDump.Close()
                    ReaDump.Close()
                    My.Computer.FileSystem.WriteAllText(TextPath & "\" & TextName & ".txt", DumpData, False, System.Text.Encoding.Default)
                    MessageBox.Show("Cotvert Successful!", "Congratulation!", MessageBoxButtons.OK)
            End Select
        Else
            MessageBox.Show("Please Select A Correct File!", "Error", MessageBoxButtons.OK)
        End If

这里给大家再讲解一下几个难点,其他的请看代码注释。

  1. 读取出来的字节必须用 Hex() 函数进行转换,否则将以十进制显示,而且对读出来的字节必须调用PadLeft过程进行format,比如如果读取出来 02,系统就会将其处理为 2,PadLeft 可以将其重新修正为 02。

  2. 我使用了 ReadCircle 记录读出的字节,用 ReadChangeLine 记录段数,大家也都知道,S50 卡都是每段 16 个字节,4 段一个扇区,我用 vbCrLf 为他们换行,还原了 Dump 原有的格式。

  3. 为什么我要用 Select Case 进行两个操作呢。因为 Dump 文件有两种标准,一种是 1 kb,一种是 4 kb,4 kb 在 1024 字节之后全空白,没必要进行转换,所以在文件流指针到达 1024 时候就退出循环。

  4. 判断 EOF 的时候不要用 peekchar()!.Net 中 peekchar() 可以预读下一位但保持指针地址不变,返回值为 -1 时表示到达文件末端,但是如果编码格式存在不同,就会出现异常,这是 .Net 的一个 Bug,所以我们用 BaseStream.Length 获取整个文件的长度,然后跟指针位置 BaseStream.Position 进行比较,文件指针位置跟文件长度相等的时候就退出循环。

程序稍微做了一下界面。如图

代码其实还是有缺陷的,但是现在还没时间去完善,等过两天在 Post 个 1.2 版本上来。这里先放个 1.1 的给大家尝尝鲜,点击文本框可以选择文件,然后点按钮就会开始转换,在 Dump 文件同个目录下生成同名 txt。注意要装 .Net 框架哦!

Download Dump2Text (Version 1.1)

Tags: 编程, RFID, .Net, Development, 开发, visual studio

2 Comments

  1. Tags 中 Visual Stdio 应为 Visual Studio

  2. TimonPeng TimonPeng

    python版代码 bin2dump.py

    #!/usr/bin/python
    from future import with_statement
    import sys
    import binascii

    READ_BLOCKSIZE = 16

    def main(argv):
    argc = len(argv)
    if argc < 3:
    print 'Usage:', argv[

    with file(argv[1], &quot;rb&quot;) as file_inp, file(argv[2], &quot;w&quot;) as file_out: while True: byte_s = file_inp.read(READ_BLOCKSIZE) if not byte_s: break hex_char_repr = binascii.hexlify(byte_s) file_out.write(hex_char_repr) file_out.write(&quot;\n&quot;)

    if name == 'main':
    main(sys.argv)

Write a new comment