הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Friday, December 5, 2014 4:37 PM
Hi,
I'm using VB 2013 (.NET 4.5)
I try to encode a zip-file to base64 byte array and send it to a web-service. The web-service is an official site and will decode it back to original state. This does not work. My byte array is probably wrong, because the service is used daily by many others, so there is nothing wrong on the receiving side.
Here is my code:
Dim _ttAttachementData As Byte()
..
_ttAttachementData = System.Text.Encoding.Default.GetBytes(System.Convert.ToBase64String(System.IO.File.ReadAllBytes(inFile)))
The zipped file is input (inFile) and _ttAttachementData is sent to the web-service.
Any suggestions?
P.O. Lien
All replies (4)
Friday, December 5, 2014 8:20 PM ✅Answered
probably like this ... Encoding.UTF8
System.Text.Encoding.UTF8.GetBytes(System.Convert.ToBase64String(System.IO.File.ReadAllBytes(inFile)))
Saturday, December 6, 2014 10:27 PM ✅Answered
So, what Crazypennie has posted is the way to convert a base64 string into an array of bytes.
Devon, What I am doing is not to create an array, it is insuring that the stream can be read as a UTF8 string.
An array created from the UTF8 encoding method GetByte when passed in a IO.Stream can be read as UTF8 encoded string
This way, if the service expect a UTF8 string, it will be able to read ... and if the service reads any kind of string, it still will be able to read it
Here an example of the process
Dim StartString As String = "CrazyPennie" 'This is an array of unicode
Dim UTF8String = System.Text.Encoding.UTF8.GetBytes(StartString) 'this is an array of UFT8
' this simulate whatever type of object that is used to pass the string to the site
Dim MS As New IO.MemoryStream(UTF8String)
Dim FinalString As String
'This simulate the service expecting a UTF8 string
Using SR As New IO.StreamReader(MS, System.Text.UTF8Encoding.UTF8)
FinalString = SR.ReadToEnd ' back to a string
End Using
'and the stream is also readable from a service that dont care of the encoding
Using SR As New IO.StreamReader(MS)
FinalString = SR.ReadToEnd ' back to a string
End Using
Doing this, ... either it works ... or if it don't, I can rule out that the encoding is the problem.
So, If the OP comes back an say "It don't work" I know that the problem is not the encoding.
Luc
Friday, December 5, 2014 11:26 PM
base64, by its definition is a method of encoding bytes that may or may not be representable by ASCII characters into a string of ASCII characters. It is also defined as the method by which those ASCII characters can be decoded into (an array of) bytes. So, what Crazypennie has posted is the way to convert a base64 string into an array of bytes.
There is "officially" no such thing as a base64 byte array, its an ASCII string.
Does that make sense ?
Sunday, December 7, 2014 6:29 AM
I thought the "rules" for base 64 were quite strict:
[A-Z], [a-z], /, +, and = as a terminator if needed. I'll have to do some reading, I am confused (not the first time)