Search code examples
vb.netaudiobase64mp3

VB.NET - Converting MP3 to Base64-String


I'm trying to find a way to store mp3-sounds in text-files. My plan was to convert the original mp3-file into a base64-string and then save it.

I spent a lot of time asking Google but could only find a way to convert the base64-string back to mp3.

Is this even possible? I am also open for other solutions, I do only have to be able to convert the files to text and then back to mp3-format. I am using Visual Basic .NET, but I think C# could help me out as well.


Solution

  • Use the Convert.ToBase64String method to convert a byte array to a base64 string:

    ' load file into a byte array
    Dim data As Byte() = File.ReadAllBytes(filename)
    ' convert the byte array to base64
    Dim str As String = Convert.ToBase64String(data)
    ' write the string to a file
    File.WriteAllText(newFilename, str)