双重转义转义 - 使用字节

  • 本文关键字:转义 字节 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

使用列表...

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

相关内容

最新更新