两次转义转义-使用字节

时间:2018-10-17 15:06:36

标签: vb.net

我需要对文件中所有可能的ascii转义进行转义。我已经写了这个,并认为它运行良好,但是只是出于某种原因注意到,文件的末尾有很多额外的字节。也许有更好的方法可以做到这一点,所以我在这里:)查找字节并在其旁边添加字节的最佳方法是什么?

Dim imageData() As Byte = File.ReadAllBytes(f_imagePath)
'Escape any ascii escapes
For i As Int32 = 0 To imageData.Length
    If imageData(i) = &H1B Then
        ReDim Preserve imageData(imageData.Length + 1)
        'shift entire array
        Dim arrCopy(imageData.Length + 1) As Byte
        Array.Copy(imageData, 0, arrCopy, 0, i)
        arrCopy(i) = &H1B
        Array.Copy(imageData, i, arrCopy, i + 1, imageData.Length - i)
        imageData = arrCopy
        i = i + 1
    End If
Next

1 个答案:

答案 0 :(得分:0)

使用列表...

    Dim imageData() As Byte = File.ReadAllBytes(f_imagePath)
    Dim newIMGData As New List(Of Byte)
    'Escape any ascii escapes
    For i As Int32 = 0 To imageData.Length
        If imageData(i) = &H1B Then
            'not sure about this,
            newIMGData.Add(imageData(i)) 'add the &H1B
            newIMGData.Add(&H0) 'add the other character
        Else
            newIMGData.Add(imageData(i))
        End If
    Next
    imageData = newIMGData.ToArray