如何将十六进制保存到文件中? [英] How do I save hex to a file?

查看:296
本文介绍了如何将十六进制保存到文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作十六进制查看器/编辑器,但是问题是保存阶段,我希望它可以将十六进制转换为文件.例如,文件包含这是测试"但是,如果我打开它,保存并打开它,就一样了.

I'm trying to make a Hex Viewer/Editor but the problem is the saving stage where I want it to convert the Hex into a file. E.g a file contains "This is a test" but if I open it, Save it and open it, it would be the same.

Imports System.IO

Public Class Form1
    Dim hexstring

    Private Sub OpenToolStripButton_Click(sender As Object, e As EventArgs) Handles OpenToolStripButton.Click
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            If My.Computer.FileSystem.GetFileInfo(OpenFileDialog1.FileName).Length / 1024 / 1024 > 7 Then
                Dim x As MsgBoxResult
                x = MsgBox("Warning! Loading a File that is 7 MB or more can take a long time depending on the specs of your computer" & vbCrLf & " and the conversion process that the program has to do to convert the bytes into hexadecimal." & vbCrLf & " Do you want to continue?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "Warning - Hex Viewer")
                If x = MsgBoxResult.Yes Then
                    Using file As New IO.FileStream(OpenFileDialog1.FileName, IO.FileMode.Open)
                        Dim value As Integer = file.ReadByte()

                        Do Until value = -1
                            hexstring = hexstring & (value.ToString("X2"))

                            value = file.ReadByte()
                        Loop
                    End Using
                    RichTextBox1.Text = hexstring
                End If
            Else
                Using file As New IO.FileStream(OpenFileDialog1.FileName, IO.FileMode.Open)
                    Dim value As Integer = file.ReadByte()

                    Do Until value = -1
                        hexstring = hexstring & (value.ToString("X2"))

                        value = file.ReadByte()
                    Loop
                End Using
                RichTextBox1.Text = hexstring
            End If
        End If
    End Sub

    Private Sub RichTextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RichTextBox1.KeyPress
        If (Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57) Or (Asc(e.KeyChar) >= 65 And Asc(e.KeyChar) <= 70) Or (Asc(e.KeyChar) >= 97 And Asc(e.KeyChar) <= 102) Or (Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 16) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub

    Private Sub SaveToolStripButton_Click(sender As Object, e As EventArgs) Handles SaveToolStripButton.Click
        Try
            If SaveFileDialog1.ShowDialog <> DialogResult.Cancel Then
                Dim bytes As Byte() = RichTextBox1.Text.Split(" "c).Select(Function(n) Convert.ToByte(Convert.ToInt32(n, 16))).ToArray()
                My.Computer.FileSystem.WriteAllBytes(SaveFileDialog1.FileName, bytes, False)
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub
End Class

谢谢!

Thanks!

Lloyd .CH.

Lloyd .CH.

推荐答案

输入文件=测试数据,输出文件=测试数据1.不要删除空格在RichTextBox中显示的十六进制值之间,或者添加多个空格或更改RichTextBox文本,使其不能另存为Hex.用作十六进制不是有效的控件 编辑器(如果您要这样做).

Input file = Test Data, output file = Test Data 1. Don't remove spaces between hex values displayed in RichTextBox or add multiple spaces or alter the RichTextBox text such that it can not be saved as Hex. It's not an efficient control to use as a Hex editor if that is what you are trying to do.

Option Strict On

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        RichTextBox1.Clear()
        Using OFD As New OpenFileDialog
            With OFD
                .Multiselect = False
                .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
                .Title = "Display text file as Hex."
                .Filter = "Text files (*.Txt)|*.Txt"
            End With
            If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim b() As Byte = My.Computer.FileSystem.ReadAllBytes(OFD.FileName)
                Dim Result As String = ""
                For i = 0 To b.Count - 1
                    If i <> b.Count - 1 Then
                        Result &= b(i).ToString("X2") & " "
                    Else
                        Result &= b(i).ToString("X2")
                    End If
                Next
                RichTextBox1.Text = Result
            End If
        End Using
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Using SFD As New SaveFileDialog
            With SFD
                .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
                .Title = "Save RichTextBox text to file as Hex."
                .Filter = "Text files (*.Txt)|*.Txt"
            End With
            If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim b As Byte() = RichTextBox1.Text.Split(" "c).Select(Function(n) Convert.ToByte(Convert.ToInt32(n, 16))).ToArray()
                My.Computer.FileSystem.WriteAllBytes(SFD.FileName, b, False)
            End If
        End Using
    End Sub

End Class

输入文件文本.

This is a test


输出文件文本.


Output file text.

This is a test


这篇关于如何将十六进制保存到文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆