Unix字符串替换

时间:2019-02-11 21:02:10

标签: vb.net

我试图通过使用简单的字节编辑来更改游戏的游戏文件,但是每当我保存它(尝试使用所有可用的编码)时,都会丢失一些字符。 有任何修复技巧吗? (顺便说一下,它仅在我替换字符串时发生,而不是在之前)

1 个答案:

答案 0 :(得分:-1)

您可以尝试使用System.IO.FileStream,然后将值读入字节数组。然后,您可以使用read方法。这是一个示例:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim fs As System.IO.FileStream
        Dim fsOut As System.IO.FileStream

        Dim path As String = "[YourInputFileLocation]"
        Dim pathOut As String = "[YourOutputFileLocation]"

        Dim b() As Byte
        Dim bytEdit() As Byte = {}
        Dim bytLeft() As Byte = {}
        Dim bytRight() As Byte = {}

        Dim strTemp As String
        Dim intPos As Integer
        Dim strFind As String
        Dim lngFileSize As Long
        Dim lngFilePos As Long
        Dim intLen As Integer
        Dim strOut As String = "NoAppliance"
        Dim blnWriteData As Boolean
        Dim blnAtEnd As Boolean

        'Open the stream and read it. 
        fs = New System.IO.FileStream(path, IO.FileMode.Open)
        fsOut = New System.IO.FileStream(pathOut, IO.FileMode.Create)

        lngFileSize = 0
        lngFilePos = 0
        lngFileSize = fs.Length
        blnAtEnd = False
        Do While Not blnAtEnd ' .Read(b, 0, b.Length) > 0
            If lngFilePos + 1024 > lngFileSize Then
                ReDim b(0 To lngFileSize - lngFilePos - 1)
                blnAtEnd = True
            Else
                ReDim b(0 To 1023)
            End If
            fs.Read(b, 0, b.Length)

            blnWriteData = False

            strTemp = BytesToString(b)

            strFind = "blender"

            intPos = InStr(strTemp, strFind)
            If intPos > 0 Then
                blnWriteData = True

                intLen = Len(strFind)

                ReDim bytLeft(0 To (intPos - 1) - 1)
                ReDim bytEdit(0 To (intLen - 1))
                ReDim bytRight(0 To (b.Length - bytLeft.Count - bytEdit.Count - 1))

                Array.ConstrainedCopy(b, 0, bytLeft, 0, bytLeft.Count)
                Array.ConstrainedCopy(b, intPos - 1, bytEdit, 0, bytEdit.Count)
                Array.ConstrainedCopy(b, intPos - 1 + intLen, bytRight, 0, bytRight.Count)

                Debug.Print("Count (1024) = " & bytLeft.Count + bytEdit.Count + bytRight.Count)
                'Edit the bytes that you wish!

                ReDim bytEdit(0 To (Len(strOut) - 1))
                bytEdit = StringToBytes(strOut)
            End If

            'Finally, Write out bytes to a new FileStream
            If blnWriteData Then
                fsOut.Write(bytLeft, 0, bytLeft.Count)
                fsOut.Write(bytEdit, 0, bytEdit.Count)
                fsOut.Write(bytRight, 0, bytRight.Count)
            Else
                fsOut.Write(b, 0, b.Count)
            End If

            lngFilePos += b.Length
        Loop

        Debug.Print(fs.Length)
        Debug.Print(fsOut.Length)

        fsOut.Close()
        fs.Close()

        fsOut = Nothing
        fs = Nothing
    End Sub

    Private Function StringToBytes(strInString As String) As Byte()
        Dim bytOut() As Byte
        Dim intI As Integer
        Dim intLen As Integer
        Dim chrIn() As Char
        Dim bytTemp() As Byte

        chrIn = strInString.ToCharArray

        intLen = Len(strInString)
        ReDim bytOut(0 To intLen - 1)
        For intI = 0 To (intLen - 1)
            bytTemp = BitConverter.GetBytes(chrIn(intI))    'CByte(AscW(Mid(strInString, intI, 1)))
            Array.ConstrainedCopy(bytTemp, 0, bytOut, intI, 1)
        Next intI

        Return bytOut
    End Function

    Private Function BytesToString(bytIn() As Byte) As String
        Dim strOut As String
        Dim intI As Integer

        strOut = ""
        For intI = 0 To UBound(bytIn)
            strOut = strOut & ChrW(bytIn(intI))
        Next intI

        Return strOut
    End Function```
相关问题