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.
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)
Related
I have a byte() array full of Unicode data. I need to convert this byte array to binary data.
The original data was binary data, but data was saved as Unicode, and thus these data structures are now all 2 times as large as they need to be.
Can I convert from a byte array of one type to another byte array, or is looping required to skip every other byte?
Edit:
Comments asked for more info
The original byte array is Unicode UTF32 looks to be the format.
The output byte array needs to remove that extra encoding.
So, assuming this, then EndianUnicode as bytes to toss out the extra data works quite well
This seems to work:
b2 = System.Text.Encoding.BigEndianUnicode.GetBytes
(System.Text.Encoding.UTF32.GetString(b))
Of course it not clear as to why the resulting array is not EXACTLY 1/2 in size but the above does seem to work.
Edit2:
Ok, as noted, the question was not ONLY how to convert, but was btye arrary to btye array. Further more, the array was indeed Unicode, but the original binary byte array was based on the users local code page (English).
So the CORRECT conversion I required was this:
b2 = System.Text.Encoding.Default.GetBytes
(System.Text.Encoding.Unicode.GetString(b))
However, the above converts from a byte arrary to a string, and then back to a byte array. My question was STILL how to do this from byte arrary to byte array. Turns out you can do this, and this is how:
Dim b() As Byte
b = reader(0) ' the array is filled with Unicode (air code)
Dim b2() As Byte
' convert byte array - not have to convert to strings
Dim cFrom As System.Text.Encoding = System.Text.Encoding.Unicode
Dim cto As System.Text.Encoding = System.Text.Encoding.Default
b2 = System.Text.Encoding.Convert(cFrom, cto, b)
As noted, above is byte() array to byte() array as per my original question.
Note that "default" in above is of course the default code page (in my case a computer running English version of windows).
Why does this work
Dim mem As New MemoryStream()
Dim bin As New BinaryWriter(mem)
bin.Write(CUShort(1000))
Dim read As New BinaryReader(New MemoryStream(mem.ToArray))
MsgBox(read.ReadInt16)
The message box give me 1000 which is right. Then I try to use this
Dim mem As New MemoryStream()
Dim bin As New BinaryWriter(mem)
bin.Write(CUShort(1000))
Dim s As String = ASCII.GetString(mem.ToArray)
Dim read As New BinaryReader(New MemoryStream(ASCII.GetBytes(s)))
MsgBox(read.ReadInt16)
It gives me 831 which is incorrect. Now I try it with Unicode encoding. It works. But I want to use ASCII. Why is this, and what am I doing wrong?
What you experience happens because of the way the .NET Runtime stores strings in memory, and because different encodings have a different set of characters.
A (U)Short is represented in memory by two bytes. When you call ASCII.GetString() the byte array is interpreted as coming from an ASCII string and is therefore converted into a UTF-16 string. This conversion is performed because UTF-16 is the encoding that all strings are stored as in memory by the .NET runtime.
Encoding.Unicode however is the same as UTF-16, so (at this point) no extra conversion is needed to store the string in memory. The byte array is just copied and marked as a string, thus you get the very same bytes and the same UShort.
This fiddle illustrates what I'm talking about: https://dotnetfiddle.net/p4EKn9
I have an application that makes a call to a third party web API that returns a String that looks something like this:
"JVBERi0xLjMNCiXi48/TDQoxIDAgb2JqDQo8PA0KL1R5cGUgL091dGxpbmVzDQovQ291bnQgMA0KPj4NCmVuZG9iag0KMiAwIG9iag0KDQpbL1BERiAvVGV4dCAvSW1hZ2VDXQ0KZW"
(It's actually much longer than that but I'm hoping just the small snippet is enough to recognize it without me pasting a string a mile long)
The documentation says it returns a Byte array but when I try to accept it as a Byte array directly, I get errors. Part of my problem here is that the documentation isn't completely clear what the Byte array represents. Since it's a GetReport function I'm calling, I'm guessing it's a PDF but I'm not 100% sure as the documentation doesn't say at all.
So, anyway, I'm getting this String and I'm trying to convert it to a PDF. Here's what that looks like:
Dim reportString As String = GetValuationReport(12345, token.SecurityToken)
Dim report As Byte() = System.Text.Encoding.Unicode.GetBytes(reportString)
File.WriteAllBytes("C:\filepath\myreport.pdf", report)
I'm pretty sure that the middle line converts the String into a new Byte array rather than simply converting it into its Byte array equivalent but I don't know how to do that.
Any help would be fantastic. Thanks!
It looks like your string may be Base64 encoded, in which case you would use this to convert it to bytes:
Dim report As Byte() = Convert.FromBase64String(reportString)
Okay, So I have been trying to wrap my small head around this topic for some time now. All I want to do is:
1) Read all data from a .json file
2) Add data to the json file, while still following structure (Adding objects withing java somehow maybe?)
3) Save file back
I have figured out how to download json.net and add it to my project. I just have no clue how to use it.
I am a big noob at java and vb, so please don't reply with a bunch of unnecessary stuff that won't help. (Really irritated by this already :|)
I am writing this with a GUI, so no console stuff, as I seen from most of the sources on the interweb.
Things I have tried: Reading all lines and storing into var, array and or string.
Dim str() As String = IO.File.ReadAllLines("C:\MCHCI_Profile.txt")
I got this from somewhere but threw and error of 1 dimensional array
Dim singleChar As Char
singleChar = str.Chars(14)
Somethings with streamreader and writer but not too much, as it confuses me.
Using sr As StreamReader = New StreamReader("C:\MCHCI_Profile.txt")
Do
ListBox1.Items.Add(sr.ReadLine())
Loop Until sr.EndOfStream
End Using
^This seemed to work, it added all the right data into combobox and kept json structure, but I don't know what to do with it.
Final conclusion
It seems like the only real way to do this is with json.net
So please let me know how to read data, add simple objects to it and save it back
Thank you !!!
As of now i don't get what actually you are trying to achieve. Let me assume that
1. You are accepting a text file content to a one dimensional array.
2. then you are selecting a single character from particular index from that array, isn't it?
this will achieve the first option without fail.
Dim str() As String = File.ReadAllLines("D:\sample.txt")
if you want particular line of text then you can take it from the array by using the index value as like the following:
Dim lineOfText As String = str(14)
if you want a single character from particular line of text then you can take it from the array by using the index value as like the following:
Dim singleChar As Char=str(14).ToCharArray()(2)
For some reason my previous question was considered too vague. So let me be more specific.
I have a 2 dimensional array of type single.
I want to serialize it to save in an Access database.
The suggestion was to save it as a Memo field which is fine.
I want to later read the Memo field and deserialize it to retrieve the original array.
I have searched extensively on the web and here and can not find the answer. I believe I am serializing the array correctly but do not know how to deserialize it.
This code appears to work for serializing but I can not figure out how to deserialize:
Dim f As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim ms As New MemoryStream
f.Serialize(ms, arLHS)
Dim byArr As Byte() = ms.ToArray
I then save byArr to the Memo field.
Please provide sample code.
You can deserialize it via base64 converter:
Dim str_b64 As String = Convert.ToBase64String(byArr)
Dim ms2 As New MemoryStream(Convert.FromBase64String(str_b64))
Dim intArr2(,) As Int32 = f.Deserialize(ms2)
This may look somewhat awkward, but it works - tested in a console app in VS 2010.
Credit goes here. Through this link, you can also find the full version of the code to play with.
I changed the data type of the Access field from Memo to OLE Object and that seems to work. When I look at the data in Access it tells me the field is "Long binary data" and I was able to use the following code after reading the record from Access:
Dim f As New System.Runtime.Serialization.Formatters.BinaryFormatter
Dim byArr As Byte()
byArr = DirectCast(dtLHS.Rows(0).Item("LHS"), Byte())
Dim theArrayAsString As String = Convert.ToBase64String(byArr)
Dim ms2 As New MemoryStream(Convert.FromBase64String(theArrayAsString))
Dim newLHS(,) as single = f.Deserialize(ms2)
Is this correct?