相关文章推荐
If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. As the name already tells you, 'WriteByte' expects a single Byte and not a String (TextBox2.Text). Convert TextBox2 to a Byte array first and then use 'Write' to write the Byte array to the correct position.
Something like this. Replace the 0 in Seek with the correct offset.
vb.net Code:
  1. Dim byteArray() As Byte = System.Text.Encoding.UTF8.GetBytes(TextBox2.Text)
  2.  
  3. Using fs As New FileStream("filepath", FileMode.Open, FileAccess.Write)
  4.     fs.Seek(0, SeekOrigin.Begin)
  5.     fs.Write(byteArray, 0, byteArray.Length)
  6. End Using
Then use the function below. Make sure that the Hex string has an even number of characters or an exception will occur.
vb.net Code:
  1. Imports System.Runtime.Remoting.Metadata.W3cXsd2001
  2.  
  3. Private Function HexStringToByteArray(ByVal hex As String) As Byte()
  4.     Dim shb As SoapHexBinary = SoapHexBinary.Parse(hex)
  5.     Return shb.Value
  6. End Function
  7.  
  8. Dim byteArray() As Byte = HexStringToByteArray(TextBox2.Text)
  9.  
  10. '// other code
vb.net Code:
  1. Private Function ByteArrayToHexString(ByVal byteArray As Byte()) As String
  2.         Dim hex As New System.Text.StringBuilder(byteArray.Length * 2)
  3.         For Each b As Byte In byteArray
  4.             hex.AppendFormat("{0:x2}", b)
  5.         Next
  6.         Return hex.ToString()
  7.     End Function
  8.  
  9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  10.         Dim byteArray(15) As Byte
  11.  
  12.         Using fs As New FileStream("filepath", FileMode.Open, FileAccess.Read)
  13.             fs.Seek(0, SeekOrigin.Begin)
  14.             fs.Read(byteArray, 0, byteArray.Length)
  15.         End Using
  16.  
  17.         Label5.Text = ByteArrayToHexString(byteArray)
  18.     End Sub
::Edit::
You should probably use "Dim byteArray(7) As Byte", as 8 Bytes will give you a Hex String of 16 characters. Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.
 
推荐文章