I have a byte array being returned by a rest service. The issue is that it is being encoded into XML. Is there anyway to extract the byte array from the XML without losing its represented file structure?
Or am I doomed to work with a string? If so is there any way to convert the string to the byte array so I can create the file it represents?
Thanks!!!!
To embed a byte array into an XML file, your best option is to encode the array into a string, via a hex encoder, or a base64 encoder. A hex encoder would accept an array like {0xEF, 0x12, 0x5A} into a string like "EF125A". The base64 encoder will do something similar, but will produce a smaller string.
Then you would do the converse in order to read the xml file.
Depending on your language and platform, there are different ways to do the encoding and decoding.
Related
[return: System.Xml.Serialization.XmlElementAttribute("return", DataType="base64Binary")]
public byte[] get(...)
I am trying to get a xml(utf-8) from this webservice. I have tried multiple things to try and get the xml out of the byte array like:
stream
encoding
decoder
converter
[Extra info]
When decoding the byte array with Encoding.UTF8.GetString(bytes)
I get a string with strange signs and symbols but also with some text
Starting with: %PDF-1.4
[SOLUTION]
Writing the byte array to a pdf file makes it readable.
I think the web service provides a byte stream that is simply base64-encoded data represented as integers instead of chars. I believe that the base64 chars are a subset of ASCII, so you need to convert the byte array to ASCII (i.e. base64 represented as chars), then convert these chars from base64:
var base64AsAscii = Encoding.ASCII.GetString(bytesFromWebService);
var decodedBytes = Convert.FromBase64String(bytesAsAscii);
var text = Encoding.UTF8.GetString(decodedBytes);
You can try Convert.ToBase64String.
Can any byte array be converted to a string? Or there are some byte values that are not available or cannot be converted to characteres depending on the encoding of the string?
You should only try to convert byte arrays to strings if they started as text. If the byte array is actually the contents of an image file, or a video, or maybe encoded or compressed data, you should not try to convert it straight to a string using an encoding. Doing so almost always goes badly in the end: with ISO-8859-1 you might be okay, but it's fundamentally a bad idea, and you really shouldn't do it.
Instead, you should use Convert.ToBase64String to convert it to Base64, or perhaps convert it to hex instead.
If you do use Base64, you'd use Convert.FromBase64String to convert back from text to a byte array.
Can any byte array be converted to a string?
Base64 seems like an appropriate representation of a byte array:
byte[] buffer = ...
string base64 = Convert.ToBase64String(buffer);
In .NET you could use the ToBase64String method to achieve this.
Also you seem to have talked about some encoding of a string in your question, but in .NET all strings are UTF-16 encoded, so I don't quite understand what you meant by that.
Strings may be converted into sequences of bytes using a variety of encodings. Some encodings can convert any possible string to some sequence of bytes; others will only work with strings containing a limited variety of characters, but for every possible byte sequence there will exist a string that would yield it. Some encoding methods will convert any possible string into an even-length sequence of bytes, and will allow any even-length sequence of bytes to be converted back to a string, but cannot yield odd-length strings. I'm not aware of any encoding methods which create a one-to-one relationship between all possible strings and all possible arbitrary-length byte sequences.
Once upon a time, strings were a convenient way of holding arbitrary byte sequences, but in .NET they may only be used as a means of holding binary data if the data is filtered so as to ensure that it doesn't contain any invalid characters or sequences. I wish there was an "immutable byte sequence" type which could be used for that purpose which used to be served by strings, but I'm unaware of one.
I'm using an old technology called RTTY to send data (it's basically fancy Morse Code) over radio.
RTTY can only transmit ascii characters
What I want to do is convert a file such as a small jpg or something similar into a block of ascii text, send the characters over radio, then convert the characters on the remote end back into the original file.
Some help getting started would be great.
I know I need to use StreamReader but then how can I convert the byte[] into an encoded ascii string that I can then 'decode'.
I know i need to use streamreader but then how can I convert the byte[] into an encoded ascii string that I can then 'decode'
Basically, you want to use a Base64 conversion. It will inflate the size of the data, but it guarantees that you'll be able to round-trip the original binary data.
Use Convert.ToBase64String to convert a byte[] into a string, and Convert.FromBase64String to do the reverse.
I'm writing a windows forms application in c#. The application allows the user to select source code-files from a listbox and displays them in colored code using ScintillaNET. The files are saved as byte arrays in a database. I've managed to make the conversion from a file on my hard drive to byte array and store it. The user should also be able to edit the code and then save it to the database without having to dowload the file to their local hard drive first, I don't know how to approach this.
Basically I want to save the text from the ScintillNET control and convert it to a byte array.
And the other way around, take a byte array and print out the text as it originally appeared in ScintillaNET.
You can use the "Encoding" class from System.Text.
System.Text.Encoding.Unicode.GetBytes("Example");
This will return a byte array with the bytes equivalent to the text "string" using the unicode encoding. There are other encoding available, but I suggest using unicode since it supports more characters (anything you find in windows charmap, for example). In my case is because I'm latin and certain letters aren't available in UTF and I have my doubts about ASCII.
Now to convert from the byte array to string use:
byte[] exampleByteArray = MemStream.ToArray();
System.Text.Encoding.Unicode.GetString(exampleByteArray);
This code will return the string saved previously as a byte array in a memory stream. You can load the byte array with other methods, in you your case you are gonna load it from the database and call System.Text.Encoding.Unicode.GetString().
I believe you are looking for the System.Text.Encoding namespace...
// a sample string...
string example = "A string example...";
// convert string to bytes
byte[] bytes = Encoding.UTF8.GetBytes(example);
// convert bytes to string
string str = System.Text.Encoding.UTF8.GetString(bytes);
I have a webservice that returns a binary data as a string. Using C# code how can I store it in byte array? Is this the right way?
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] bytes = encoding.GetBytes(inputString);
Actually, this didn't work. Let me explain it more:
the web service code converts a string (containing XSLFO data) into byte array using utf8 encoding. In my web service response I only see data something like "PGZvOnJvb3QgeG1sbnM6Zm89Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvWFNML0Zvcm1hdCIgeG1sbnMÂ6eGY9Imh0dHA6Ly93d3cuZWNyaW9uLmNvbS94Zi8xLjAiIHhtbG5zOm1zeHNsPSJ1c==". Actually I would like to have the original string value that was converted into byte[] in the service. Not sure if it possible?
No, that's a bad idea.
Unless the input data was originally text, trying to use Encoding is a bad idea. The web service should be using something like base64 to encode it - at which point you can use Convert.FromBase64String to get the original binary data back.
Basically, treating arbitrary binary data as if it were encoded text is a quick way to lose data. When you need to represent binary data in a string, you should use base64, hex or something similar.
This may mean you need to change the web service as well, of course - if it's creating the string by simply treating the binary data as UTF-8-encoded text, it's broken to start with.
If the string is encoded in UTF8 Encoding, then yes that is the correct way. If it is in Unicode it is very similar:
System.Text.Unicode encoding = new System.Text.Unicode();
byte[] bytes = encoding.GetBytes(inputString);
Base64Encoding is a little different:
byte[] bytes = Convert.FromBase64String(inputString);