.Net Core IFormFile convert to hexadecimal - asp.net-core

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.

Related

Image corrupt when convert base64 string to image in asp.net core

I am trying to convert base64 string to image in .net core but the resulted image is corrupted
Following is my code
if (!String.IsNullOrEmpty(model.Image))
{
string filePath = Path.Combine(_hostingEnvironment.ContentRootPath+"/Image" , model.ImageName);
System.IO.File.WriteAllBytes(filePath, Convert.FromBase64String(model.Image));
}
is there any other method to create image from base64 string and save to server in asp.net core?
I have tried your code and it works well.You could try below correct base64string to have a test and check whether you have a correct base64string
var image = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wgARCAGQAZADASIAAhEBAxEB/8QAGgABAQEAAwEAAAAAAAAAAAAAAQIAAwUGBP/EABkBAQEBAQEBAAAAAAAAAAAAAAABAgMEBf/aAAwDAQACEAMQAAAB9RKevzTkqSgkSpKEkRQQmaCZoJKkBxIgCEiBKAIAgCEiBsLhDbBhA2xhD2omZJRUlBIhJU2AgCEzckiElSAgTQsiEjICAIAgCAIAgCLhDGDbBtse1yZhNFSUElSAlklSSUElBE3KyUJIgTUhNSTNyslFSIAkAgFSBUgIYR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ruA95zm24OLzBPcMxzke0s+QH9cY4ZMycM8+JmScZ5ZJ5xy92Jksbv/9k=";
var bytes = Convert.FromBase64String(image);
string filePath = Path.Combine(_hostingEnvironment.ContentRootPath + "/Image" , "testImage.jpeg");
using (var imageFile = new FileStream(filePath, FileMode.Create))
{
imageFile.Write(bytes, 0, bytes.Length);
imageFile.Flush();
}

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.

Getting around base64 encoding with WCF

I'm using WCF, REST and "pretty URI's" as shown in this blog post with the Online Template for VS 2010 .NET 4.0:
http://christopherdeweese.com/blog2/post/drop-the-soap-wcf-rest-and-pretty-uris-in-net-4
I have one problem though.
I want to return a a raw byte[] array but it automatically gets base64 encoded.
Unfortunately for my program base64 encoding is not acceptable because it will be too computationally intensive.
Is there a way for me to tell WCF NOT to base64 encode?
[WebGet(UriTemplate = "{id}")]
public byte[] Get(string id)
{
byte[] data = new byte[1024];
return data;
}
Appears to my web browser as:
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==
Use Stream as your return type.

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.

Silverlight Byte[] to its original file format

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!