File to string and back again

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bonzol

    #1

    File to string and back again

    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

  • Tom Shelton

    #2
    Re: File to string and back again


    Bonzol wrote:
    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\Cof fee 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(I NPUT_FILE)
    ReDim buffer(CInt(fst ream.Length - 1))

    fstream.Read(bu ffer, 0, buffer.Length)
    End Using

    ' convert to a base64 string
    Dim stringValue As String = Convert.ToBase6 4String(buffer)
    Console.WriteLi ne(stringValue)

    ' now convert it back to a byte array
    buffer = Convert.FromBas e64String(strin gValue)

    ' write it to the new file
    Using fstream As FileStream = File.OpenWrite( OUTPUT_FILE)
    fstream.Write(b uffer, 0, buffer.Length)
    End Using

    End Sub

    End Module

    Comment

    Working...