Bonzol said:
vb.net 1.1 Windows application
Hey,, In a bit of a hurry,, does anyone know a way to turn a file in to
a string,, such as a jpeg or pdf,, then turn that string back in to the
original file?
Cheers
Sure... Read the binary data into a byte array. Base64 encode it.
You know have a string. To get it back to the file, decode it to a
byte array and write the data to a file...
Option Strict On
Option Explicit On
Imports System
Imports System.IO
Module Module1
Private Const INPUT_FILE As String = "c:\WINDOWS\Coffee Bean.bmp"
Private Const OUTPUT_FILE As String = "Coffee Bean.bmp"
Sub Main()
Dim buffer() As Byte
' read the bitmap
Using fstream As FileStream = File.OpenRead(INPUT_FILE)
ReDim buffer(CInt(fstream.Length - 1))
fstream.Read(buffer, 0, buffer.Length)
End Using
' convert to a base64 string
Dim stringValue As String = Convert.ToBase64String(buffer)
Console.WriteLine(stringValue)
' now convert it back to a byte array
buffer = Convert.FromBase64String(stringValue)
' write it to the new file
Using fstream As FileStream = File.OpenWrite(OUTPUT_FILE)
fstream.Write(buffer, 0, buffer.Length)
End Using
End Sub
End Module