2014-07-11 4 views
1

음, 사실 나는 내 코드에서 이것을 사용계산 UTF8의 readpos

Public Sub WriteString(ByVal Input As String) 
    Buff.AddRange(BitConverter.GetBytes(Input.Length)) 
    Buff.AddRange(Encoding.Unicode.GetBytes(Input)) 
End Sub 
Public Function ReadString(Optional ByVal Peek As Boolean = True) As String 
    Dim Len As Integer = ReadInteger(True) * 2 
    Dim ret As String = Encoding.Unicode.GetString(Buff.ToArray, readpos, Len) 
    If Peek And Buff.Count > readpos Then 
     If ret.Length > 0 Then 
      readpos += Len 
     End If 
    End If 
    Return ret 
End Function 

기능 ReadInteger :

내가 UTF8 유니 코드를 변경하려면
Public Function ReadInteger(Optional ByVal peek As Boolean = True) As Integer 
    If Buff.Count > readpos Then 'check to see if this passes the byte count 
     Dim ret As Integer = BitConverter.ToInt32(Buff.ToArray, readpos) 
     If peek And Buff.Count > readpos Then 
      readpos += 4 
     End If 
     Return ret 
    Else 
     Throw New Exception("Byte Buffer Past Limit!") 'past byte count throw a new exception 
    End If 
End Function 

, 누군가가 조언 해결책을 가지고 있습니까?

답변

1

BinaryReader/Writer를 강력하게 고려해야합니다.

당신이 다음 바이트, 문자열의 길이가 아닙니다 작성하여 문제를 해결하는 데 도움이 붙어 있다면 그러나 :

Public Sub WriteString(ByVal Input As String) 
    Dim bytes = Encoding.UTF8.GetBytes(Input) 
    Buff.AddRange(BitConverter.GetBytes(bytes.Length)) 
    Buff.AddRange(bytes) 
End Sub 

Public Function ReadString(Optional ByVal Peek As Boolean = True) As String 
    Dim bytes = ReadInteger(True) 
    Dim str = Encoding.UTF8.GetString(Buff.ToArray, readpos, bytes) 
    readpos += bytes 
    Return str 
End Function 

이 Buff.ToArray 못생긴주의 마을,이 생성 너무 많은 쓰레기. Buff이 있으면 MemoryStream이면 GetBuffer()를 대신 사용하십시오.