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:
Dim byteArray() As Byte = System.Text.Encoding.UTF8.GetBytes(TextBox2.Text)
Using fs As New FileStream("filepath", FileMode.Open, FileAccess.Write)
fs.Seek(0, SeekOrigin.Begin)
fs.Write(byteArray, 0, byteArray.Length)
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:
Imports System.Runtime.Remoting.Metadata.W3cXsd2001
Private Function HexStringToByteArray(ByVal hex As String) As Byte()
Dim shb As SoapHexBinary = SoapHexBinary.Parse(hex)
Return shb.Value
End Function
Dim byteArray() As Byte = HexStringToByteArray(TextBox2.Text)
'// other code
vb.net Code:
Private Function ByteArrayToHexString(ByVal byteArray As Byte()) As String
Dim hex As New System.Text.StringBuilder(byteArray.Length * 2)
For Each b As Byte In byteArray
hex.AppendFormat("{0:x2}", b)
Next
Return hex.ToString()
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim byteArray(15) As Byte
Using fs As New FileStream("filepath", FileMode.Open, FileAccess.Read)
fs.Seek(0, SeekOrigin.Begin)
fs.Read(byteArray, 0, byteArray.Length)
End Using
Label5.Text = ByteArrayToHexString(byteArray)
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.