Silverlight Byte[] to its original file format - silverlight-4.0

is there a way to convert byte[] to its original file format?
Byte[] tempByte = new Byte[content.Length];
tempByte = Convert.FromBase64String(content);

If you have a Base64 encoded string, then yes Convert.FromBase64String will give you back a byte array identical to the one that was converted to a Base64 string.
However, your first line is unnecessary. You are allocating an array equal to the length of content which just gets overwritten by the return value from Convert.FromBase64String.
byte[] tempByte = Convert.FromBase64String(content);
File.WriteAllBytes(path, tempByte);

The byte array should already be having what you originally read from the file. Write the byte array to a file on the disk and you should be good to go!

Related

.Net Core IFormFile convert to hexadecimal

I have a project in Asp Net Core 3.1. I want to convert the downloaded mp3 files (of the IFormFile type) to hexadecimal.
What is the best way to do this?
For converting IFormFile to hexadecimal, first IFormFile to byte array conversion is needed. Later, It can easily be converted to a hexadecimal string.
Code:
public async Task FileToHexaConversion(IFormFile file)
{
await using var memoryStream = new MemoryStream();
await file.CopyToAsync(memoryStream);
var byteArray = memoryStream.ToArray(); //Byte Array Conversion
string hexString = BitConverter.ToString(byteArray); //Hexadecimal String Conversion
Console.WriteLine(hexString.Replace('-', ' '));
}
Note: Here, I replaced the hyphen separator with space.

newtonsoft SerializeXmlNode trailing nulls

I am creating an XmlDoc in C# and using Newtonsoft to serialize to JSON. It works, but I am getting a bunch of what appear to be "NUL"'s at the end of the JSON. No idea why. Anyone seen this before?
CODE:
XmlDocument xmlDoc = BuildTranslationXML(allTrans, applicationName, language);
// Convert the xml doc to json
// the conversion inserts \" instead of using a single quote, so we need to replace it
string charToReplace = "\"";
string jsonText = JsonConvert.SerializeXmlNode(xmlDoc);
// json to a stream
MemoryStream memoryStream = new MemoryStream();
TextWriter tw = new StreamWriter(memoryStream);
tw.Write(jsonText);
tw.Flush();
tw.Close();
// output the stream as a file
string fileName = string.Format("{0}_{1}.json", applicationName, language);
return File(memoryStream.GetBuffer(), "text/json", fileName);
The file is served up to the calling web page and the browser prompts the user to save the file. When opening the file, it displays the correct JSON but also has all the trailing nulls. See image below (hopefully the stackoverflow link works):
file screenshot
The GetBuffer() method returns the internal representation of the MemoryStream. Use ToArray() instead to get just the part of that internal array that has data Newtonsoft has put in there.

vb- networkstream write only returns first result (need all)

My code looks very similar to this post Read bytes from NetworkStream (Hangs), which I copied below. ( I realize this is c# - I need a vb solution)
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
TcpClient client = new TcpClient(server, port);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Get a client stream for reading and writing.
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", message);
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
// Close everything.
stream.Close(); client.Close();
My issue lies in this:
I have a form that takes user input on which tif file(s) to find (which ends up being the getBytes(message) ). When one does this, it always returns one result - the first tif file that fits the criteria, However, I know in certain instances I should recieve more than one match.
Then I send the results to a picturebox and should be able to scroll through results (which this part works fine)
I've tried a few ways to get multiple results, but maybe I'm missing the obvious? My best guess is using the asyncronous beginread/write....
I've attempted using a for loop but I end up getting a bunch of the same tif files as a result...
Could anyone help me (even with a generic direction)? I'm not a pro. Thanks in advance for the help.
I ended up finding out what the issue was. the data i was looking for wasn't standardized as so the results were the same data over and over. I changed what items I was looking for and tada it worked. anyway, thanks for the help

How to write Linq Binary type to MemoryStream and vice versa

I'm going to write System.Data.Linq.Binary value to MemoryStream and perform some manipulations, then re-write new values from MemoryStream to Binary! how to do?
You can't modify a Binary instance, because it's immutable (the MSDN documentation says: "Represents an immutable block of binary data."). But you can assign a new value to a Binary variable:
Binary binary = ...
// Binary to MemoryStream
MemoryStream stream = new MemoryStream(binary.ToArray());
...
// MemoryStream to binary
binary = stream.ToArray(); // implicit conversion from byte[] to Binary

decompression in c#

i have compressed an xml file and converted it into base64 format in web page.then i have passed the base64 string to windows application and converted it to unbase64 format.now i want to decompress this string in windows form only.
i have done the following in windows
dim decoded as byte()
decoded=convert.frombase64string(strreturndata) // strreturndata is base64 string
dim decoders as string
decoders = encoding.utf8.getstring(decoded)
now i want to decompress this string using gzip stream class in windows form
If you want to get back the Base64 string, use this.
Try this:
byte[] x = Convert.FromBase64String(decodedString);
string mytext = System.Encoding.utf8.getstring(x, 0, x.length)
If you need to do more compression, you can use Gzip compression and decompression or DeflateStream class.