Novahq.net Forum

Novahq.net Forum (https://novahq.net/forum/index.php)
-   Web design and Programming (https://novahq.net/forum/forumdisplay.php?f=32)
-   -   VB.net Hex Editing (https://novahq.net/forum/showthread.php?t=42592)

--BulletMagnet-- 01-18-2009 03:11 PM

VB.net Hex Editing
 
This is a little more difficult.

Code:

Public Class Form1
    Private Shared Function HexStringToByteArray(ByRef strInput As String)
As Byte()
        Dim length As Integer
        Dim bOutput As Byte()
        Dim c(1) As Integer
        length = strInput.Length / 2
        ReDim bOutput(length - 1)
        For i As Integer = 0 To (length - 1)
            For j As Integer = 0 To 1
                c(j) = Asc(strInput.Chars(i * 2 + j))
                If ((c(j) >= Asc("0")) And (c(j) <= Asc("9"))) Then
                    c(j) = c(j) - Asc("0")
                ElseIf ((c(j) >= Asc("A")) And (c(j) <= Asc("F"))) Then
                    c(j) = c(j) - Asc("A") + &HA
                ElseIf ((c(j) >= Asc("a")) And (c(j) <= Asc("f"))) Then
                    c(j) = c(j) - Asc("a") + &HA
                End If
            Next j
            bOutput(i) = (c(0) * &H10 + c(1))
        Next i
        Return (bOutput)
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
        Dim myFile As String = "..\..\Test.jpg"
        Dim myBytes As Byte() =
My.Computer.FileSystem.ReadAllBytes(myFile)
        Dim txtTemp As New System.Text.StringBuilder()
        For Each myByte As Byte In myBytes
            txtTemp.Append(myByte.ToString("X2"))
        Next
        RichTextBox1.Text = txtTemp.ToString()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
        Dim myFile As String = "..\..\Test2.jpg"
        Dim myBytes As Byte() = HexStringToByteArray(RichTextBox1.Text)
        My.Computer.FileSystem.WriteAllBytes(myFile, myBytes, False)
    End Sub
End Class

Copyright (c) 2008 Bullet Magnet. All Wrongs Reserved.

ghack2k 11-19-2012 01:37 PM

This works very well but...
 
Hello,

this code works very well but ONLY for files under 2GB...

if we use this in files bigger we will get an "out of memory" exception..

I was digging and searching arround and i did realize that we can read this in chunks to avoid this exception... But i couldn't realize how to use the chunks and add to this code.

Does anyone knows how to do it? How can we read a file bigger than 2GB without giving the "out of memory" exception ?

Thanks in advanced.:gj:

ghack2k 11-19-2012 02:02 PM

This works very well but...
 
Hello,

this works very well but ONLY for files under 2GB... if we choose any file bigger it will throw an "out of memory" exception :sad5:

I have digging and searching arround and i know that we can make the file/bytes reading by chunks to avoid the "out of memory" error. However i couldn't realize how to do this or how to adapt this to this code.:huh:

Does anyone knows how can we read files bigger that 2GB without errors? Does anyone knows how to adapt the chunks reading here?

Thanks in advanced.:gj:


All times are GMT -5. The time now is 03:16 PM.

Powered by vBulletin®