Convert jpeg's to HEX

G

Guest

I want to convert jpeg's to Hex but I don't really seem to get it right.
The code I use is the following:

Private Function tmpGetByteFileFromDisk(ByVal FilePath As String) As Byte()
Try
Dim fs As FileStream = New FileStream(FilePath, FileMode.Open,
FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)
Dim i As Integer
Dim photo As String
br.Read()
Dim p As String
For i = 0 To 40697
p = Hex(br.ReadByte)
If Len(p) = 1 Then
photo = photo & "0" & p
Else
photo = photo & p
End If
Next
TextBox1.Text = photo
br.Close()
fs.Close()

I want this done to insert the image into a rtf-document using the
streamwriter object, this way I can generate rtf-documents without all the
overhead of starting the word.application.

Suggestions greatly appreciated.
 
D

David Browne

dontGetStuckLikeMe said:
I want to convert jpeg's to Hex but I don't really seem to get it right.
The code I use is the following:

Private Function tmpGetByteFileFromDisk(ByVal FilePath As String) As
Byte()
Try
Dim fs As FileStream = New FileStream(FilePath, FileMode.Open,
FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)
Dim i As Integer
Dim photo As String
br.Read()
Dim p As String
For i = 0 To 40697
p = Hex(br.ReadByte)
If Len(p) = 1 Then
photo = photo & "0" & p
Else
photo = photo & p
End If
Next
TextBox1.Text = photo
br.Close()
fs.Close()

I want this done to insert the image into a rtf-document using the
streamwriter object, this way I can generate rtf-documents without all the
overhead of starting the word.application.

Suggestions greatly appreciated.

Here's a suggestion:

Private Function FileToHex(ByVal FilePath As String) As String

Dim fs As FileStream = New FileStream(FilePath, _
FileMode.Open, _
FileAccess.Read)
Dim buf(CInt(fs.Length)) As Byte
fs.Read(buf, 0, buf.Length)
fs.Close()
Static hexChars() As Char = {"0"c, "1"c, "2"c, "3"c, "4"c, "5"c,
"6"c, _
"7"c, "8"c, "9"c, "A"c, "B"c, "C"c, "D"c,
"E"c, "F"c}


Dim sb As New Text.StringBuilder(buf.Length * 2)
For Each b As Byte In buf
Dim n1 As Integer = b >> 4
Dim n2 As Integer = b And 15
sb.Append(hexChars(n1))
sb.Append(hexChars(n2))
Next
Return sb.ToString


End Function

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top